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() {
// ???
this.tasks = []
};

newTodoList.prototype.add = function(task){
var new_task = new Task(task)
this.tasks.push(new_task);
var index = this.tasks.indexOf(new_task);
new_task.id = index + 1;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using this.tasks.length + 1 allows you to avoid line 8

};

newTodoList.prototype.list = function(){
for(var i = 0; i < this.tasks.length; i++){
console.log(this.tasks[i]);
}
}

newTodoList.prototype.remove = function(task){
var id = task.id - 1;
this.tasks.splice(id, 1);
}


var Task = function(description){
this.id = NaN;
this.description = description;
this.completed = false;
};

Task.prototype.complete = function(){
this.completed = true;
};




var todoList = newTodoList();

// Note we are using a JavaScript constructor now.
var groceryList = new newTodoList();
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}


// Driver code
// This should remove the task from the todo list
groceryList.remove(breadTask);


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