Skip to content

Instantly share code, notes, and snippets.

@imparvez
Created September 25, 2017 13:16
Show Gist options
  • Save imparvez/adc36f5b402a8820158da7fe066b788d to your computer and use it in GitHub Desktop.
Save imparvez/adc36f5b402a8820158da7fe066b788d to your computer and use it in GitHub Desktop.
// V6 Thinking in CODE.
// .toggleAll: if everything's true, make everything false.
// .toggleAll: Otherwise, make everything true.
var todoList = {
todos: [],
displayTodos: function(){
if(this.todos.length === 0) { // Chp no: 2 .displayTodos should tell you if .todos is empty
console.log('Your todo list is EMPTY');
} else {
console.log('My Todos: ');
for(var i = 0; i < this.todos.length; i++) {
if(this.todos[i].completed) { // Chp no: 3 .displayTodos should show .completed
console.log('(x)', this.todos[i].todoText); // Chp no: 1 .displayTodos should show .todoText
} else {
console.log('( )',this.todos[i].todoText); // Chp no: 1 .displayTodos should show .todoText
}
}
}
},
addTodos: function(todoText){
this.todos.push({
todoText: todoText,
completed: false
});
this.displayTodos();
},
changeTodo: function(pos, todoText) {
this.todos[pos].todoText = todoText;
this.displayTodos();
},
deleteTodo: function(pos) {
this.todos.splice(pos, 1);
this.displayTodos();
},
toggleCompleted: function(pos){
var todo = this.todos[pos];
todo.completed = !todo.completed;
this.displayTodos();
},
toggleAll: function(){
// Chp no: 1 .toggleAll: if everything's true, make everything false.
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++;
}
}
// Case 1: If everything's true, make everything false.
if(completedTodos === totalTodos) {
for(var i = 0; i < totalTodos; i++) {
this.todos[i].completed = false;
}
// Case 2: Otherwise, make everything true.
} else {
for(var i = 0; i < totalTodos; i++) {
this.todos[i].completed = true;
}
}
this.displayTodos();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment