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
75 changes: 72 additions & 3 deletions todo_list.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,79 @@
var newTodoList = function() {
// ???
var TodoList = function() {
this.id = 1
this.tasks = [],
this.add = function(description) {
this.tasks.push(new Task(description, this.id))
this.id +=1
},
this.list = function() {
for(var i=0 ; i<this.tasks.length ; i++) {
console.log(this.tasks[i])
}
},
this.remove = function(task) {
for(var i=0; i<this.tasks.length ; i++) {
if(task == this.tasks[i]) {
this.tasks.splice(i, 1)
}
}
}
};

var Task = function(description, id) {

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



// Driver code

var groceryList = new TodoList();
groceryList.add('bread');
groceryList.add('cheese');
groceryList.add('milk');

// tasks is now an array of Task objects
console.log(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];

console.log(breadTask)

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

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.add(breadTask);

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

var todoList = newTodoList();