diff --git a/Sprint-3/todo-list/index.html b/Sprint-3/todo-list/index.html index 4d12c4654..78e0c8724 100644 --- a/Sprint-3/todo-list/index.html +++ b/Sprint-3/todo-list/index.html @@ -34,7 +34,7 @@

My ToDo List

- + diff --git a/Sprint-3/todo-list/script.mjs b/Sprint-3/todo-list/script.mjs index ba0b2ceae..2b86f5386 100644 --- a/Sprint-3/todo-list/script.mjs +++ b/Sprint-3/todo-list/script.mjs @@ -13,6 +13,11 @@ window.addEventListener("load", () => { Todos.addTask(todos, "Do the shopping", true); render(); + const deleteButton = document.getElementById("delete-completed-tasks"); + deleteButton.addEventListener("click", () => { + Todos.deleteCompleted(todos); + render(); + }); }); @@ -73,4 +78,5 @@ function createListItem(todo, index) { }); 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..bfdaf75f7 100644 --- a/Sprint-3/todo-list/todos.mjs +++ b/Sprint-3/todo-list/todos.mjs @@ -26,4 +26,11 @@ export function toggleCompletedOnTask(todos, taskIndex) { if (todos[taskIndex]) { todos[taskIndex].completed = !todos[taskIndex].completed; } -} \ No newline at end of file +} +export function deleteCompleted(todoList) { + for (let i = todoList.length - 1; i >= 0; i--) { + if (todoList[i].completed) { + todoList.splice(i, 1); + } + } +} diff --git a/Sprint-3/todo-list/todos.test.mjs b/Sprint-3/todo-list/todos.test.mjs index bae7ae491..30f65ded8 100644 --- a/Sprint-3/todo-list/todos.test.mjs +++ b/Sprint-3/todo-list/todos.test.mjs @@ -130,3 +130,27 @@ describe("toggleCompletedOnTask()", () => { }); }); +describe("deleteCompleted()",()=>{ + test("remove all completed tasks",()=>{ + const todoList=createMockTodos() + Todos.deleteCompleted(todoList) + expect(todoList).toHaveLength(2) + expect(todoList).toEqual([ + { task: "Task 2 description", completed: false }, + { task: "Task 4 description", completed: false }, + ]); + }) +}); + test("does nothing when there are no completed tasks", () => { + const todoList = [ + { task: "Task 1 description", completed: false }, + { task: "Task 2 description", completed: false }, + ]; + + Todos.deleteCompleted(todoList); + + expect(todoList).toEqual([ + { task: "Task 1 description", completed: false }, + { task: "Task 2 description", completed: false }, + ]); + });