-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20180602a_PJS_objects-v8.html
154 lines (127 loc) · 4.1 KB
/
20180602a_PJS_objects-v8.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h1>Todo List</h1>
<div>
<button onclick="handlers.displayTodos()"> Display Todos </button>
<button onclick ="handlers.toggleAll()">Toggle All </button>
</div>
<div>
<button onclick="handlers.addTodo()"> Add </button>
<input id ="addTodoTextInput" type="text">
</div>
<div>
<button onclick="handlers.changeTodo()">Change Todo</button>
<input id="changeTodoPositionInput" type="number">
<input id="changeTodoTextInput" type="text">
</div>
<div>
<button onclick='handlers.deleteTodo()'> Delete </button>
<input id='deleteTodoPositionInput' type='number'>
</div>
</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();
}
};
//refactoring
//refactor
var handlers ={
displayTodos: function(){
todoList.displayTodos();
},
toggleAll: function(){
todoList.toggleAll();
},
addTodo: function(){
var addTodoTextInput= document.getElementById('addTodoTextInput');
todoList.addTodo(addTodoTextInput.value);
},
changeTodo:function (){
var changeTodoPositionInput= document.getElementById('changeTodoPositionInput');
var changeTodoTextInput = document.getElementById('changeTodoTextInput');
todoList.changeTodo(changeTodoPositionInput.valueAsNumber,changeTodoTextInput.value);
changeTodoPositionInput.value='';
changeTodoTextInput.value='';
//doesn't display changed text, instead shows undefined
},
deleteTodo: function(){
var deleteToDoPositionInput = document.getElementById('deleteToDoPositionInput');
todoList.deleteTodo(deleteToDoPositionInput.valueAsNumber);
deleteToDoPositionInput.value='';
}
};
</script>
</html>