Skip to content

Instantly share code, notes, and snippets.

@glw
Last active November 13, 2017 05:14
Show Gist options
  • Save glw/7443a5de9f413ce860453e584b9b3120 to your computer and use it in GitHub Desktop.
Save glw/7443a5de9f413ce860453e584b9b3120 to your computer and use it in GitHub Desktop.
todoList
var todoList = {
todos: [],
addTodo: function(todoText) {
this.todos.push({
todoText: todoText,
completed: false
});
},
changeTodo: function(position, todoText) {
this.todos[position].todoText = todoText;
},
deleteTodo: function(position) {
this.todos.splice(position, 1);
},
toggleCompleted: function(position) {
var todo = this.todos[position];
todo.completed = !todo.completed;
},
toggleAll: function() {
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++;
}
}
Improve for loops with forEach method
*/
// find total number of completed todos
this.todos.forEach(function(todo) {
if (todo.completed === true) {
completedTodos++
}
});
/*
**** Refactor this code below, line 66 ****
// Case 1: If everythings true, make everything false.
if (completedTodos === totalTodos) {
// for (var i = 0; i < totalTodos; i++) {
// this.todos[i].completed = false;
// }
// Improve for loop with forEach method
this.todos.forEach(function(todo) {
todo.completed = false;
});
// Case 2: Otherwise, make everything true.
} else {
// Improve with forEach method
// for (var i = 0; i < totalTodos; i++) {
// this.todos[i].completed = true;
// }
this.todos.forEach(function(todo) {
todo.completed = true;
});
*/
this.todos.forEach(function(todo) {
//Case 1: if everythings true make false
if (completedTodos === totalTodos) {
todo.completed = false;
// Case 2: otherwise make true
} else {
todo.completed = true;
}
});
}
};
var handlers = {
addTodo: function() {
var addTodoTextInput = document.getElementById('addTodoTextInput');
todoList.addTodo(addTodoTextInput.value);
addTodoTextInput.value = '';
view.displayTodos();
},
changeTodo: function() {
var changeTodoPositionInput = document.getElementById('changeTodoPositionInput');
var changeTodoTextInput = document.getElementById('changeTodoTextInput');
todoList.changeTodo(changeTodoPositionInput.valueAsNumber, changeTodoTextInput.value);
changeTodoPositionInput.value = '';
changeTodoTextInput.value = '';
view.displayTodos();
},
deleteTodo: function(position) {
todoList.deleteTodo(position);
view.displayTodos();
},
toggleCompleted: function() {
var toggleCompletedPositionInput = document.getElementById('toggleCompletedPositionInput');
todoList.toggleCompleted(toggleCompletedPositionInput.valueAsNumber);
toggleCompletedPositionInput.value = '';
view.displayTodos();
},
toggleAll: function() {
todoList.toggleAll();
view.displayTodos();
}
};
var view = {
displayTodos: function() {
var todosUl = document.querySelector('ul');
todosUl.innerHTML = '';
/* Improve with forEach mothod
for (var i = 0; i < todoList.todos.length; i++) {
var todoLi = document.createElement('li');
var todo = todoList.todos[i];
var todoTextWithCompletion = '';
if (todo.completed === true) {
todoTextWithCompletion = '(x) ' + todo.todoText;
} else {
todoTextWithCompletion = '( ) ' + todo.todoText;
}
todoLi.id = i;
todoLi.textContent = todoTextWithCompletion;
todoLi.appendChild(this.createDeleteButton());
todosUl.appendChild(todoLi);
}
*/
todoList.todos.forEach(function(todo, position){
var todoLi = document.createElement('li');
var todoTextWithCompletion = '';
if (todo.completed === true) {
todoTextWithCompletion = '(x) ' + todo.todoText;
} else {
todoTextWithCompletion = '( ) ' + todo.todoText;
}
todoLi.id = position;
todoLi.textContent = todoTextWithCompletion;
todoLi.appendChild(this.createDeleteButton());
todosUl.appendChild(todoLi);
}, this)
},
createDeleteButton: function() {
var deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.className = 'deleteButton';
return deleteButton;
},
setUpEventListeners: function() {
var todosUl = document.querySelector('ul');
todosUl.addEventListener('click', function(event) {
//console.log(event.target.parentElement.id);
//get the element that was clicked on
var elementClicked = event.target;
//check if element is a delete button.
if (elementClicked.className === 'deleteButton') {
handlers.deleteTodo(parseInt(elementClicked.parentNode.id));
}
});
}
};
view.setUpEventListeners();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment