From f95bb0b56457becd8f36074bfcdae65b3656b4b8 Mon Sep 17 00:00:00 2001 From: Adam Fluke & Tim Cannady Date: Mon, 13 Jul 2015 12:29:13 -0700 Subject: [PATCH] complete implementation of interface --- todo_list.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 3 deletions(-) diff --git a/todo_list.js b/todo_list.js index 110fce6..7dd44cf 100644 --- a/todo_list.js +++ b/todo_list.js @@ -1,10 +1,81 @@ -var newTodoList = function() { - // ??? +function search_by_task(task, task_array){ + for (var i=0; i < task_array.length; i++) { + if (task_array[i] === task) { + return i; + } + } + } + +function Task(item_name, tasks_counter) { + this.id = tasks_counter + this.description = item_name; + this.completed = false; +} + +Task.prototype.complete = function() { + this.completed = true; +} + +function TodoList() { + this.tasks = []; + this.tasks_counter = 1 }; +TodoList.prototype.add = function(item_name) { + this.tasks.push(new Task(item_name, this.tasks_counter)); + this.tasks_counter ++; +} +TodoList.prototype.list = function() { + this.tasks.forEach(function (value, index){ + console.log(value) + }) +} + +TodoList.prototype.remove = function(task_object) { + var index = search_by_task(task_object, this.tasks); + this.tasks.splice(index,1); +} + // Driver code +// Note we are using a JavaScript constructor now. +var groceryList = new TodoList(); +groceryList.add('bread'); +groceryList.add('cheese'); +groceryList.add('milk'); + +// tasks is now an array of Task objects +groceryList.tasks //-> [Task, Task, Task] + +groceryList.list(); +//> Task {id: 1, description: 'bread', completed: false} +//> Task {id: 2, description: 'cheese', completed: false} +//> Task {id: 3, description: 'milk', completed: false} + + +// getting a task object +var breadTask = groceryList.tasks[0]; + +breadTask.id //-> 1 (some unique numerical ID) +breadTask.description //-> 'bread' +breadTask.completed //-> false + + +// This should complete the task +breadTask.complete(); + +breadTask.completed //-> true + +groceryList.list(); +//> Task {id: 1, description: 'bread', completed: true} +//> Task {id: 2, description: 'cheese', completed: false} +//> Task {id: 3, description: 'milk', completed: false} + +// This should remove the task from the todo list +groceryList.remove(breadTask); -var todoList = newTodoList(); \ No newline at end of file +groceryList.list(); +//> Task {id: 2, description: 'cheese', completed: false} +//> Task {id: 3, description: 'milk', completed: false}