Skip to content

Instantly share code, notes, and snippets.

@RichardSPrins
Created March 14, 2019 01:49
Show Gist options
  • Save RichardSPrins/efbd877598b6ce73e4dd314b394a958a to your computer and use it in GitHub Desktop.
Save RichardSPrins/efbd877598b6ce73e4dd314b394a958a to your computer and use it in GitHub Desktop.
Practical JS - Watch and Code
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<h1>Hello Scott!</h1>
</body>
</html>
var todoList = {
todos: [],
displayTodos: function() {
if (this.todos.length === 0) {
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 === true) {
console.log("(x)", this.todos[i].todoText);
} else {
console.log("( )", this.todos[i].todoText);
}
}
}
},
addTodo: function(todoText) {
this.todos.push({
todoText: todoText,
completed: false
});
this.displayTodos();
},
changeTodo: function(position, todoText) {
this.todos[position].todoText = todoText;
this.displayTodos();
},
deleteTodo: function(position) {
this.todos.splice(position, 1);
this.displayTodos();
},
toggleCompleted: function(position) {
var todo = this.todos[position];
todo.completed = !todo.completed;
this.displayTodos();
},
toggleAll: function() {
var totalTodos = this.todos.length;
var completedTodos = 0;
for (var i = 0; i < totalTodos; i++) {
if (this.todos[i].completed === true) {
completedTodos++;
}
}
if (completedTodos === totalTodos) {
for (var i = 0; i < totalTodos; i++) {
this.todos[i].completed = false;
}
} else {
for (var i = 0; i < totalTodos; i++) {
this.todos[i].completed = true;
}
}
this.displayTodos();
}
};
/*Style Goes Here*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment