Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions todo_list.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,73 @@

function task(item, id) {
this.id = id,
this.description = item,
this.completed = false,
this.complete = function(){
this.completed = true;
}
}


var newTodoList = function() {
// ???
taskList: function taskList(){
this.tasks = [],
this.add = function(item) {
this.tasks.push(new task(item, this.tasks.length+1))
},
this.list = function(){ return this.tasks }
// this.remove = function(task){delete this.tasks[task.id]}
this.remove = function(task){this.tasks.splice(task.id-1,1)}
}

return taskList
};




// Driver code


var todoList = newTodoList();
var todoList = newTodoList();

// 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);

groceryList.list();
//> Task {id: 2, description: 'cheese', completed: false}
//> Task {id: 3, description: 'milk', completed: false}