From aa6fd34b302fb6edc451fe4c28bc9d4c84b3ad6b Mon Sep 17 00:00:00 2001 From: Jaclyn & Marisa Ramon Date: Mon, 3 Aug 2015 16:48:18 -0700 Subject: [PATCH 1/2] can add and list items in the todo list and complete them --- que_hacer.html | 10 +++++++++ que_hacer.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ todo_list.js | 33 +++++++++++++++++++++++++--- 3 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 que_hacer.html create mode 100644 que_hacer.js diff --git a/que_hacer.html b/que_hacer.html new file mode 100644 index 0000000..9cbc93e --- /dev/null +++ b/que_hacer.html @@ -0,0 +1,10 @@ + + + + Que Hacer List + + + + + + diff --git a/que_hacer.js b/que_hacer.js new file mode 100644 index 0000000..6c3617c --- /dev/null +++ b/que_hacer.js @@ -0,0 +1,58 @@ + +// Todo is a format that I want to use 10000000 times +var Todo = function(){ + this.tasks = [] +}; + + var Task = function(newTask) { + this.id = 0, + this.description = newTask, + this.completed = false +} + +// set of functions that ALL instances of the Todo object will inherit +Task.prototype.complete = function() { + this.completed = true +} + +Todo.prototype.add = function(newTask) { + var task = new Task(newTask) + this.tasks.push(task) + task.id = this.tasks.length +}; + +// display each task in the instance's array +Todo.prototype.list = function () { + for (var i in this.tasks) { + console.log(this.tasks[i]) + } + return this.id + "and" + this.description +} + +// instance of our Todo +var groceryList = new Todo(); +groceryList.add("chocolate") +groceryList.add("bread") +groceryList.add("peanut butter") +groceryList.list(); + +var yourPantry = new Todo(); +yourPantry.add("rice") +yourPantry.add("potatos") +yourPantry.list(); + +var partyStuff = new Todo(); + +// go get the rice and check it off on the list which is stored as an array +var completeRice = yourPantry.tasks[0] + + +// console.log("chocolate" === groceryList.tasks[1]) + + + + +// another way to see what is going on with just addTask and just list +// Todo.prototype = { +// addTask: function() {}, +// list: function() {}, diff --git a/todo_list.js b/todo_list.js index 110fce6..cb5fc7f 100644 --- a/todo_list.js +++ b/todo_list.js @@ -1,10 +1,37 @@ -var newTodoList = function() { - // ??? +NewTodoList = function() { + this.tasks = [] }; +NewTodoList.prototype.add = function(item) { + this.tasks.push(item); +} + +NewTodoList.prototype.list = function() { + NewObject = function(id, description) { + var printTasks = { + id: id, + description: description, + completed: false, + } + } +} // Driver code -var todoList = newTodoList(); \ No newline at end of file +var todoList = new NewTodoList(); + +todoList.add('banana'); +todoList.add('cheese'); +console.log(todoList.tasks) +todoList.list(); + + +// this creates another copy of newTodoList +// gives us access to the tasks array outside the object literal +// var cookies = new NewTodoList(); +// cookies.add('chocolate', 30); + +// console.log(cookies.tasks) + From 3dd9eaf15c8e156a06a91e68b47d4b2e9756bda4 Mon Sep 17 00:00:00 2001 From: Jaclyn & Marisa Ramon Date: Mon, 3 Aug 2015 17:42:52 -0700 Subject: [PATCH 2/2] remove object from the todo object --- que_hacer.js | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/que_hacer.js b/que_hacer.js index 6c3617c..266d562 100644 --- a/que_hacer.js +++ b/que_hacer.js @@ -21,12 +21,25 @@ Todo.prototype.add = function(newTask) { task.id = this.tasks.length }; +// outside the Todo object literal to avoid making a copy EACH time +// you have a new task + +Todo.prototype.remove = function(deleteMe) { + for (var i=0; i<=this.tasks.length; i++) { + console.log(this.tasks[i]) + console.log(deleteMe) + if (deleteMe === this.tasks[i]){ + console.log("I just deleted: "); + this.tasks.splice(i,1); + } + } +} + // display each task in the instance's array Todo.prototype.list = function () { for (var i in this.tasks) { - console.log(this.tasks[i]) + console.log(this.tasks[i]); } - return this.id + "and" + this.description } // instance of our Todo @@ -36,19 +49,34 @@ groceryList.add("bread") groceryList.add("peanut butter") groceryList.list(); +// expect to remove chocolate from the array +// groceryList.remove("chocolate") +// groceryList.remove("bread") +// groceryList.list(); + + var yourPantry = new Todo(); yourPantry.add("rice") yourPantry.add("potatos") yourPantry.list(); -var partyStuff = new Todo(); // go get the rice and check it off on the list which is stored as an array -var completeRice = yourPantry.tasks[0] +//gets the 'rice' from the tasks array +var completeRice = yourPantry.tasks[0]; +//expect to see the ID associated with rice +// completeRice.id +// completeRice.description +completeRice.complete(); +console.log(completeRice) +// delete rice from the list +yourPantry.remove(completeRice) -// console.log("chocolate" === groceryList.tasks[1]) +// should return FALSE in console and it does! +// console.log("chocolate" !== groceryList.tasks[1]) +var partyStuff = new Todo();