From dc722408b3b3c02afb0675c4dbb32862172402c9 Mon Sep 17 00:00:00 2001 From: k3vinyan & Phil Borel Date: Mon, 3 Aug 2015 16:35:19 -0700 Subject: [PATCH 1/2] adds constructor, list function --- todo_list.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/todo_list.js b/todo_list.js index 110fce6..6448e5a 100644 --- a/todo_list.js +++ b/todo_list.js @@ -1,10 +1,76 @@ -var newTodoList = function() { - // ??? -}; +var NewTodoList = function() { + this.tasks = []; + this.counter = 0; +} +NewTodoList.prototype.add = function(item) { + var task = { + id: this.counter += 1, + description: item, + completed: false + } + return this.tasks.push(task) + } +NewTodoList.prototype.list = function() { + // for(i = 0; i <= this.tasks.length; i++) { + this.tasks.forEach( function(task) { + console.log(task) + }); + // } +} + +// class People +// def initialize +// tasks = [] +// end + // def add(task) + // tasks << task + // end +// end +// list: function() { +// for(i = 0; i <= this.tasks.length; i++) { +// console.log(this.tasks[i]) +// } +// }, +// // indexOf: function(element) { +// // this.tasks.indexOf(element) +// // }, +// indexOf: function(element) { +// for(i= 0; i <= this.tasks.length; i++) { +// if(this.tasks[i].description === element) { +// return i +// } +// } +// }, +// get: function(index) { +// this.tasks[index]; +// }, +// remove: function(index) { +// this.tasks.splice(index, 1); +// }, +// complete: function(index) { +// this.tasks[index].completed = true +// } +// } + +// return todoList; +// } // Driver code +todoList = new NewTodoList(); + +// console.log(todoList.tasks) + +todoList.add("kevin") +todoList.add("phil") +todoList.add("phil") +todoList.add("phil") +todoList.add("phil") +todoList.add("phil") + +// console.log(todoList.tasks) + +todoList.list() -var todoList = newTodoList(); \ No newline at end of file From 8fb9168739254b18883b53ef77a719227c3fa7dd Mon Sep 17 00:00:00 2001 From: k3vinyan & Phil Borel Date: Mon, 3 Aug 2015 17:00:14 -0700 Subject: [PATCH 2/2] finish challenge --- todo_list.js | 88 +++++++++++++++++++++------------------------------- 1 file changed, 35 insertions(+), 53 deletions(-) diff --git a/todo_list.js b/todo_list.js index 6448e5a..a947992 100644 --- a/todo_list.js +++ b/todo_list.js @@ -1,69 +1,38 @@ -var NewTodoList = function() { +var TodoList = function() { this.tasks = []; this.counter = 0; } -NewTodoList.prototype.add = function(item) { - var task = { - id: this.counter += 1, - description: item, - completed: false - } + +TodoList.prototype.add = function(item) { + var task = new Task(item) + task.id = this.counter += 1 return this.tasks.push(task) } -NewTodoList.prototype.list = function() { - // for(i = 0; i <= this.tasks.length; i++) { - this.tasks.forEach( function(task) { - console.log(task) - }); - // } +TodoList.prototype.list = function() { + this.tasks.forEach( function(task) { + console.log(task) + }); } -// class People -// def initialize -// tasks = [] -// end - // def add(task) - // tasks << task - // end -// end -// list: function() { -// for(i = 0; i <= this.tasks.length; i++) { -// console.log(this.tasks[i]) -// } -// }, -// // indexOf: function(element) { -// // this.tasks.indexOf(element) -// // }, -// indexOf: function(element) { -// for(i= 0; i <= this.tasks.length; i++) { -// if(this.tasks[i].description === element) { -// return i -// } -// } -// }, -// get: function(index) { -// this.tasks[index]; -// }, -// remove: function(index) { -// this.tasks.splice(index, 1); -// }, -// complete: function(index) { -// this.tasks[index].completed = true -// } -// } - -// return todoList; -// } +// ---------------- Task Class ---------------------- -// Driver code +var Task = function(item) { + this.id = 0; + this.description = item; + this.completed = false; +} -todoList = new NewTodoList(); +Task.prototype.complete = function() { + this.completed = true +} -// console.log(todoList.tasks) +// Driver code -todoList.add("kevin") +todoList = new TodoList(); + +todoList.add("bread") todoList.add("phil") todoList.add("phil") todoList.add("phil") @@ -73,4 +42,17 @@ todoList.add("phil") // console.log(todoList.tasks) todoList.list() +var breadTask = todoList.tasks[0] +console.log(breadTask.id) //-> 1 (some unique numerical ID) +console.log(breadTask.description) //-> 'bread' +console.log(breadTask.completed) //-> false + + +// This should complete the task +breadTask.complete(); + +breadTask.completed //-> true + +todoList.list(); +