Skip to content
Open
Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
68 changes: 64 additions & 4 deletions todo_list.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,70 @@
var newTodoList = function() {
// ???
var Task = function(item) {
this.id = 0;
this.description = item;
this.completed = false;
}

Task.prototype.complete = function() {
console.log("finished this task YOOOO!");
this.completed = true;
}

var TodoList = function() {
this.tasks = [];
this.counter = 0;
};

TodoList.prototype.add = function(item) {
var newItem = new Task(item);
newItem.id = this.counter += 1;
this.tasks.push(newItem);
}

TodoList.prototype.remove = function(taskObj) {
for (var i = 0; i < this.tasks.length; i++) {
if (taskObj === this.tasks[i]) {
console.log("removed this task YOOO!");
this.tasks.splice(i, 1);
}
}
}

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

//var todoList = newTodoList();
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(); // function() { this.completed = true}

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}