From 573c08e0ea9c0e69ef61cf38502c2ec25c595c5e Mon Sep 17 00:00:00 2001 From: 1UnboundedSentience & HoomanGriz Date: Mon, 3 Aug 2015 17:28:52 -0700 Subject: [PATCH] implement methods using prototypical inheritance, moved all methods to prototype except tasks and a counter so that the array of task objects could be modified when the methods were called --- .DS_Store | Bin 0 -> 6148 bytes todo_list.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..dea57e564d73a0d7c5ba8062cda83a11700e683c GIT binary patch literal 6148 zcmeHKO-sW-5PhRPR21pa;~qT~{0Fh5LJwZ*4^V0kDk(Ws@A-lLr}*YWX)rfIL}p;- zZFcAFCJ(Z^13(sU+bduOU`kgMZH$<9j}9G0@-b0tj$14-#}3<7GcwU%?9z8X!J6-V zjT#5<-#%`dcEeGX=7xURo3RC);}tEo^m{aT#-7h}JTdOjteOG$UAc7Wd!^!Rl>37H zkSA;A9gWkP4&%slbmF;F+y9J$1~O3Zw$5z?TBLKNPxR71%l2*1=#U0CB=@ zGq&}@teg^91$K@cp@~z8PL(Jz#OdrWkyizFj!uU}@gcGDXYnGoI_EEz4yhb7rUI$J zPywxdX)`_lm;7Z$i+oGTkP4&%|5X8*)OYoQUl(WVx9`-mw$LBwYMR$+gU0sY62J%D gM=os8$BVX^R|R&CI*axjotPH^6C^__@Cyoj0M*SiZU6uP literal 0 HcmV?d00001 diff --git a/todo_list.js b/todo_list.js index 110fce6..3c33781 100644 --- a/todo_list.js +++ b/todo_list.js @@ -1,10 +1,70 @@ -var newTodoList = function() { - // ??? +var Task = function(item) { + this.id = 0; + this.description = item; + this.completed = false; +} + +Task.prototype.complete = function() { + console.log("finished this task YOOOO!"); + this.completed = true; +} + +var TodoList = function() { + this.tasks = []; + this.counter = 0; }; +TodoList.prototype.add = function(item) { + var newItem = new Task(item); + newItem.id = this.counter += 1; + this.tasks.push(newItem); +} + +TodoList.prototype.remove = function(taskObj) { + for (var i = 0; i < this.tasks.length; i++) { + if (taskObj === this.tasks[i]) { + console.log("removed this task YOOO!"); + this.tasks.splice(i, 1); + } + } +} + +TodoList.prototype.list = function() { + for(var i = 0; i < this.tasks.length; i++) { + console.log(this.tasks[i]); + } +} + +//var todoList = newTodoList(); +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(); // function() { this.completed = true} + +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} -// Driver code +// 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}