-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathtodo_list.js
42 lines (33 loc) · 852 Bytes
/
todo_list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var Task = function(todoList, description) {
this.todoList = todoList;
this.id = Task.counter++;
this.description = description;
this.completed = false;
}
Task.prototype.complete = function(){
this.completed = true;
}
Task.prototype.remove = function(){
var index = this.todoList.tasks.indexOf(this);
this.todoList.tasks.splice(index, 1);
}
Task.counter = 1;
var TodoList = function() {
this.tasks = [];
};
TodoList.prototype.add = function(description){
var task = new Task(this, description)
this.tasks.push(task)
}
TodoList.prototype.list = function(){
for (i=0; i < this.tasks.length; i++) {
console.log(this.tasks[i]);
};
}
// Driver code
var myTodoList = new TodoList();
myTodoList.add("Go to movie");
myTodoList.add("Go to gym");
movie = myTodoList.tasks[0];
movie.remove();
console.log(myTodoList.list());