Skip to content

Commit 63e615a

Browse files
Implemented the populateTodoList, addNewTodo, and deleteAllCompletedTodos functions
1 parent ce620b7 commit 63e615a

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

Sprint-3/todo-list/script.js

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,73 @@
11
function populateTodoList(todos) {
22
let list = document.getElementById("todo-list");
3-
// Write your code to create todo list elements with completed and delete buttons here, all todos should display inside the "todo-list" element.
3+
list.innerHTML = "";
4+
5+
todos.forEach((todo) => {
6+
let li = document.createElement("li");
7+
li.textContent = todo.task;
8+
li.className = `list-group-item ${todo.completed ? "completed" : ""}`;
9+
10+
let span = document.createElement("span");
11+
span.className = "badge bg-light rounded-pill";
12+
13+
let checkIcon = document.createElement("i");
14+
checkIcon.className = "fa fa-check todo-icon";
15+
checkIcon.setAttribute("aria-hidden", "true");
16+
checkIcon.addEventListener("click", () => {
17+
todo.completed = !todo.completed;
18+
li.classList.toggle("completed");
19+
});
20+
21+
let trashIcon = document.createElement("i");
22+
trashIcon.className = "fa fa-trash todo-icon";
23+
trashIcon.setAttribute("aria-hidden", "true");
24+
trashIcon.addEventListener("click", () => {
25+
const index = todos.indexOf(todo);
26+
if (index > -1) {
27+
todos.splice(index, 1);
28+
}
29+
populateTodoList(todos);
30+
});
31+
32+
span.appendChild(checkIcon);
33+
span.appendChild(trashIcon);
34+
35+
li.appendChild(span);
36+
37+
list.appendChild(li);
38+
});
439
}
540

6-
// These are the same todos that currently display in the HTML
7-
// You will want to remove the ones in the current HTML after you have created them using JavaScript
841
let todos = [
942
{ task: "Wash the dishes", completed: false },
1043
{ task: "Do the shopping", completed: false },
1144
];
1245

13-
populateTodoList(todos);
14-
15-
// This function will take the value of the input field and add it as a new todo to the bottom of the todo list. These new todos will need the completed and delete buttons adding like normal.
1646
function addNewTodo(event) {
17-
// The code below prevents the page from refreshing when we click the 'Add Todo' button.
1847
event.preventDefault();
19-
// Write your code here... and remember to reset the input field to be blank after creating a todo!
48+
let input = document.getElementById("todoInput");
49+
let newTask = input.value.trim();
50+
51+
if (/[^A-Za-z\s]/.test(newTask)) {
52+
alert("Please enter letters and spaces only.");
53+
input.value = "";
54+
return;
55+
}
56+
57+
if (newTask !== "") {
58+
todos.push({ task: newTask, completed: false });
59+
populateTodoList(todos);
60+
input.value = "";
61+
}
2062
}
63+
populateTodoList(todos);
2164

22-
// Advanced challenge: Write a fucntion that checks the todos in the todo list and deletes the completed ones (we can check which ones are completed by seeing if they have the line-through styling applied or not).
2365
function deleteAllCompletedTodos() {
24-
// Write your code here...
66+
todos = todos.filter((todo) => !todo.completed);
67+
populateTodoList(todos);
2568
}
69+
70+
document.getElementById("todo-form").addEventListener("submit", addNewTodo);
71+
document
72+
.getElementById("remove-all-completed")
73+
.addEventListener("click", deleteAllCompletedTodos);

0 commit comments

Comments
 (0)