+
diff --git a/Sprint-3/todo-list/script.mjs b/Sprint-3/todo-list/script.mjs
index ba0b2ceae..b4f5c435d 100644
--- a/Sprint-3/todo-list/script.mjs
+++ b/Sprint-3/todo-list/script.mjs
@@ -1,4 +1,4 @@
-// Store everything imported from './todos.mjs' module as properties of an object named Todos
+// Store everything imported from './todos.mjs' module as properties of an object named Todos
import * as Todos from "./todos.mjs";
// To store the todo tasks
@@ -9,14 +9,20 @@ window.addEventListener("load", () => {
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);
// Populate sample data
- Todos.addTask(todos, "Wash the dishes", false);
+ Todos.addTask(todos, "Wash the dishes", false);
Todos.addTask(todos, "Do the shopping", true);
+ document
+ .getElementById("delete-completed-btn")
+ .addEventListener("click", () => {
+ Todos.deleteCompleted(todos);
+ render();
+ });
+
render();
});
-
-// A callback that reads the task description from an input field and
+// A callback that reads the task description from an input field and
// append a new task to the todo list.
function addNewTodo() {
const taskInput = document.getElementById("new-task-input");
@@ -45,12 +51,11 @@ function render() {
});
}
-
// Note:
// - First child of #todo-item-template is a
element.
// We will create each ToDo list item as a clone of this node.
// - This variable is declared here to be close to the only function that uses it.
-const todoListItemTemplate =
+const todoListItemTemplate =
document.getElementById("todo-item-template").content.firstElementChild;
// Create a
element for the given todo task
@@ -62,15 +67,15 @@ function createListItem(todo, index) {
li.classList.add("completed");
}
- li.querySelector('.complete-btn').addEventListener("click", () => {
+ li.querySelector(".complete-btn").addEventListener("click", () => {
Todos.toggleCompletedOnTask(todos, index);
render();
});
-
- li.querySelector('.delete-btn').addEventListener("click", () => {
+
+ li.querySelector(".delete-btn").addEventListener("click", () => {
Todos.deleteTask(todos, index);
render();
});
return li;
-}
\ No newline at end of file
+}
diff --git a/Sprint-3/todo-list/todos.mjs b/Sprint-3/todo-list/todos.mjs
index f17ab6a25..248f09504 100644
--- a/Sprint-3/todo-list/todos.mjs
+++ b/Sprint-3/todo-list/todos.mjs
@@ -26,4 +26,10 @@ export function toggleCompletedOnTask(todos, taskIndex) {
if (todos[taskIndex]) {
todos[taskIndex].completed = !todos[taskIndex].completed;
}
-}
\ No newline at end of file
+}
+
+export function deleteCompleted(todoList) {
+ const completed = todoList.filter((todo) => !todo.completed);
+ todoList.length = 0;
+ todoList.push(...completed);
+}
diff --git a/Sprint-3/todo-list/todos.test.mjs b/Sprint-3/todo-list/todos.test.mjs
index bae7ae491..a26246f36 100644
--- a/Sprint-3/todo-list/todos.test.mjs
+++ b/Sprint-3/todo-list/todos.test.mjs
@@ -130,3 +130,12 @@ describe("toggleCompletedOnTask()", () => {
});
});
+test("deleteCompleted removes all completed tasks", () => {
+ const todoList = [
+ { task: "Wash dishes", completed: true },
+ { task: "Do shopping", completed: false },
+ { task: "Read book", completed: true },
+ ];
+ Todos.deleteCompleted(todoList);
+ expect(todoList).toEqual([{ task: "Do shopping", completed: false }]);
+});