From 63911ccb06696552eb8fa33a101493b8b944edd1 Mon Sep 17 00:00:00 2001 From: John Hess & Nicholas Toulouse Date: Mon, 13 Jul 2015 14:58:19 -0700 Subject: [PATCH 1/2] first draft of OOJS todoList, now need to create method .add() --- todo_list.js | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/todo_list.js b/todo_list.js index 110fce6..39f77ea 100644 --- a/todo_list.js +++ b/todo_list.js @@ -1,10 +1,33 @@ -var newTodoList = function() { - // ??? -}; +// var newTodoList = function() { +// // ??? +// }; +function Task(item, val) { + this.id = val; + this.description = item; + this.completed = false; +} -// Driver code +function TodoList(tasks) { + if(!tasks) { tasks = []; } + this.tasks = tasks; + var counter = this.tasks.length; +} + +TodoList.prototype.list = function() { + for (var i = 0; i < this.tasks.length; i++) { + console.log("Task : " + JSON.stringify(this.tasks[i]) + "\n"); + } +} + +TodoList.prototype.complete = function() { +} + +// Driver code -var todoList = newTodoList(); \ No newline at end of file +var cheese = new Task("cheese", 1); +var bread = new Task("bread", 2); +var groceryList = new TodoList([cheese, bread]); +groceryList.list(); From bf64aee4627fac19668ae1909e948378ef532351 Mon Sep 17 00:00:00 2001 From: John Hess & Nicholas Toulouse Date: Mon, 13 Jul 2015 15:51:06 -0700 Subject: [PATCH 2/2] OOJS release 2 completion --- todo_list.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/todo_list.js b/todo_list.js index 39f77ea..0c5dde9 100644 --- a/todo_list.js +++ b/todo_list.js @@ -3,6 +3,10 @@ // }; +// TodoList.prototype.add - function that takes a val and item as parameters. +// It creates a new Task object, passing in the parameters +// It adds the Task to the tasks array + function Task(item, val) { this.id = val; this.description = item; @@ -15,14 +19,30 @@ function TodoList(tasks) { var counter = this.tasks.length; } +TodoList.prototype.add = function (item, val) { + this.tasks.push(new Task(item, val)); +} + TodoList.prototype.list = function() { for (var i = 0; i < this.tasks.length; i++) { console.log("Task : " + JSON.stringify(this.tasks[i]) + "\n"); } } -TodoList.prototype.complete = function() { +TodoList.prototype.complete = function(taskname) { + for (var i = 0; i < this.tasks.length; i++) { + if(this.tasks[i].description == taskname){ + this.tasks[i].completed = true; + } + } +} +TodoList.prototype.remove = function(taskObj) { + for (var i = 0; i < this.tasks.length; i++) { + if(this.tasks[i] === taskObj){ + this.tasks.splice(i, 1); + } + } } // Driver code @@ -30,4 +50,8 @@ TodoList.prototype.complete = function() { var cheese = new Task("cheese", 1); var bread = new Task("bread", 2); var groceryList = new TodoList([cheese, bread]); +groceryList.add("lettuce", 3); groceryList.list(); +groceryList.complete("lettuce"); +bread.completed = false; +console.log(bread.completed);