-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20180530a_PJS_objects-debugger.html
126 lines (100 loc) · 3.12 KB
/
20180530a_PJS_objects-debugger.html
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h1>Todo List</h1>
<button id="displayTodosButton"> Display Todos </button>
<button id = 'toggleAllButton' >Toggle All </button>
</body>
<script>
// Code goes here
//make a change
//v7
//1
var todoList={
todos:[],
displayTodos: function(){
//debugger;
//if there are no todos, (todos.length is equal to 0, ===0) console.log('your todo list is empty');
if(this.todos.length===0){
console.log("there is no todo");
} else {
console.log("My Todos:"); //remove ', this.todos'
for(var i=0;i<this.todos.length;i++){
if (this.todos[i].completed===true){
console.log('(X)',this.todos[i].todoText);
} else{
console.log('( )',this.todos[i].todoText);
}
}
}
},
addTodo:function(todoText){
//debugger;
this.todos.push({
todoText:todoText,
completed:false
}); //defined earlier
this.displayTodos(); // defined earlier
},
changeTodo: function(position, newValue){
//debugger;
this.todos[position]=newValue;
this.displayTodos();
},
deleteTodo: function(position){
//debugger;
this.todos.splice(position, 1);
this.displayTodos();
},
toggleCompleted: function(position){
//debugger;
var todo=this.todos[position];
todo.completed = !todo.completed;
this.displayTodos();
},
toggleAll: function(){
debugger;
var totalTodos = this.todos.length;
var completedTodos = 0;
//get number of completed todos
for (var i=0; i<totalTodos;i++){
if(this.todos[i].completed ===true){
completedTodos++;
}
}
//if everything's true, make everything false
if(completedTodos === totalTodos){
// make everything false
for(var i=0; i<totalTodos; i++){
this.todos[i].completed = false;
}
} //case2: otherwise make everything tru
else {
for(var i=0; i<totalTodos;i++){
this.todos[i].completed = true;
}
}
this.displayTodos();
}
};
//function changeTodo(position, newValue){
// todos[position]= new Value;
// displayTodos();
//}
//1.we want to get access to the display todos button
var displayTodosButton = document.getElementById('displayTodosButton');
var toggleAllButton = document.getElementById('toggleAllButton');
//console.log(displayTodosButton);
//2. we want to run displayTodos method, when someone clicks the display todos button
displayTodosButton.addEventListener('click', function(){
todoList.displayTodos();
});
toggleAllButton.addEventListener('click', function (){
todoList.toggleAll();
})
</script>
</html>