From 689f6e54fa274fa966ae630f184dc23641f3884a Mon Sep 17 00:00:00 2001 From: Maharit Belai Date: Sun, 22 Nov 2026 09:48:51 +0000 Subject: [PATCH 01/11] added a deletecompleted function --- Sprint-3/todo-list/todos.mjs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Sprint-3/todo-list/todos.mjs b/Sprint-3/todo-list/todos.mjs index f17ab6a250..7d4b84baeb 100644 --- a/Sprint-3/todo-list/todos.mjs +++ b/Sprint-3/todo-list/todos.mjs @@ -26,4 +26,13 @@ export function toggleCompletedOnTask(todos, taskIndex) { if (todos[taskIndex]) { todos[taskIndex].completed = !todos[taskIndex].completed; } -} \ No newline at end of file +} +export function deleteCompleted(todos) { + for (let i = 0; i < todos.length; i++) { + if (todos[i].completed) { + todos.splice(i, 1); + i--; + } + } + return todos; +} From d4633d8cdbfcf8bc938c548ed566181292103512 Mon Sep 17 00:00:00 2001 From: Maharit Belai Date: Sun, 22 Nov 2026 09:49:19 +0000 Subject: [PATCH 02/11] added a test for the function it works fine --- Sprint-3/todo-list/todos.test.mjs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Sprint-3/todo-list/todos.test.mjs b/Sprint-3/todo-list/todos.test.mjs index bae7ae4917..d1f7995523 100644 --- a/Sprint-3/todo-list/todos.test.mjs +++ b/Sprint-3/todo-list/todos.test.mjs @@ -13,7 +13,7 @@ function createMockTodos() { { task: "Task 1 description", completed: true }, { task: "Task 2 description", completed: false }, { task: "Task 3 description", completed: true }, - { task: "Task 4 description", completed: false }, + { task: "Task 4 description", completed: false }, ]; } @@ -29,7 +29,6 @@ describe("addTask()", () => { }); test("Should append a new task to the end of a ToDo list", () => { - const todos = createMockTodos(); const lengthBeforeAddition = todos.length; Todos.addTask(todos, theTask.task, theTask.completed); @@ -42,7 +41,6 @@ describe("addTask()", () => { }); describe("deleteTask()", () => { - test("Delete the first task", () => { const todos = createMockTodos(); const todosBeforeDeletion = createMockTodos(); @@ -53,7 +51,7 @@ describe("deleteTask()", () => { expect(todos[0]).toEqual(todosBeforeDeletion[1]); expect(todos[1]).toEqual(todosBeforeDeletion[2]); - expect(todos[2]).toEqual(todosBeforeDeletion[3]); + expect(todos[2]).toEqual(todosBeforeDeletion[3]); }); test("Delete the second task (a middle task)", () => { @@ -66,7 +64,7 @@ describe("deleteTask()", () => { expect(todos[0]).toEqual(todosBeforeDeletion[0]); expect(todos[1]).toEqual(todosBeforeDeletion[2]); - expect(todos[2]).toEqual(todosBeforeDeletion[3]); + expect(todos[2]).toEqual(todosBeforeDeletion[3]); }); test("Delete the last task", () => { @@ -79,7 +77,7 @@ describe("deleteTask()", () => { expect(todos[0]).toEqual(todosBeforeDeletion[0]); expect(todos[1]).toEqual(todosBeforeDeletion[1]); - expect(todos[2]).toEqual(todosBeforeDeletion[2]); + expect(todos[2]).toEqual(todosBeforeDeletion[2]); }); test("Delete a non-existing task", () => { @@ -94,7 +92,6 @@ describe("deleteTask()", () => { }); describe("toggleCompletedOnTask()", () => { - test("Expect the 'completed' property to toggle on an existing task", () => { const todos = createMockTodos(); const taskIndex = 1; @@ -111,13 +108,11 @@ describe("toggleCompletedOnTask()", () => { const todos = createMockTodos(); const todosBeforeToggle = createMockTodos(); Todos.toggleCompletedOnTask(todos, 1); - - expect(todos[0]).toEqual(todosBeforeToggle[0]); + expect(todos[0]).toEqual(todosBeforeToggle[0]); expect(todos[2]).toEqual(todosBeforeToggle[2]); expect(todos[3]).toEqual(todosBeforeToggle[3]); }); - test("Expect no change when toggling on a non-existing task", () => { const todos = createMockTodos(); const todosBeforeToggle = createMockTodos(); @@ -129,4 +124,12 @@ describe("toggleCompletedOnTask()", () => { expect(todos).toEqual(todosBeforeToggle); }); }); - +test("should return the tasks that are not completed", () => { + const todos = createMockTodos(); + const todosBeforeDeleteComplet = createMockTodos(); + const lengthBeforDeleteComplete = todos.length; + Todos.deleteCompleted(todos); + expect(todos).toHaveLength(lengthBeforDeleteComplete - 2); + expect(todos[0]).toEqual(todosBeforeDeleteComplet[1]); + expect(todos[1]).toEqual(todosBeforeDeleteComplet[3]); +}); From 4b6be6bb73729efc3526011c0ae35a2e03382dc9 Mon Sep 17 00:00:00 2001 From: Maharit Belai Date: Sun, 22 Nov 2026 11:27:10 +0000 Subject: [PATCH 03/11] added the button for Delete completed task --- Sprint-3/todo-list/index.html | 73 ++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/Sprint-3/todo-list/index.html b/Sprint-3/todo-list/index.html index 4d12c46543..877170fb94 100644 --- a/Sprint-3/todo-list/index.html +++ b/Sprint-3/todo-list/index.html @@ -1,40 +1,51 @@ - + - - - - ToDo List - - + + + + ToDo List + + - - - -
-

My ToDo List

+ + + +
+

My ToDo List

-
- - -
+
+ + + +
-
    -
+
    - - - -
    - + +
    + From ab92ee2d9d8c7f5bc162beed153abc6e688ffa5d Mon Sep 17 00:00:00 2001 From: Maharit Belai Date: Sun, 22 Nov 2026 11:28:12 +0000 Subject: [PATCH 04/11] Added the event listner that is trigered by delete completed Button --- Sprint-3/todo-list/script.mjs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Sprint-3/todo-list/script.mjs b/Sprint-3/todo-list/script.mjs index ba0b2ceaec..e9591b9139 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,19 @@ 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); render(); + document + .getElementById("delete-complete-btn") + .addEventListener("click", () => { + Todos.deleteCompleted(todos); + 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 +50,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 +66,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 +} From ad37a8735d0e0d4b2bb1e44aa9ad981c3355272c Mon Sep 17 00:00:00 2001 From: Maharit Belai Date: Sun, 22 Nov 2026 11:57:29 +0000 Subject: [PATCH 05/11] checked to make sure it ignores the DeleteIeCompletedfunction return value by commenting out my test fails --- Sprint-3/todo-list/todos.test.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-3/todo-list/todos.test.mjs b/Sprint-3/todo-list/todos.test.mjs index d1f7995523..f8042fee7e 100644 --- a/Sprint-3/todo-list/todos.test.mjs +++ b/Sprint-3/todo-list/todos.test.mjs @@ -120,7 +120,7 @@ describe("toggleCompletedOnTask()", () => { Todos.toggleCompletedOnTask(todos, 10); expect(todos).toEqual(todosBeforeToggle); - Todos.toggleCompletedOnTask(todos, -1); + // Todos.toggleCompletedOnTask(todos, -1); expect(todos).toEqual(todosBeforeToggle); }); }); @@ -128,7 +128,7 @@ test("should return the tasks that are not completed", () => { const todos = createMockTodos(); const todosBeforeDeleteComplet = createMockTodos(); const lengthBeforDeleteComplete = todos.length; - Todos.deleteCompleted(todos); + //Todos.deleteCompleted(todos); expect(todos).toHaveLength(lengthBeforDeleteComplete - 2); expect(todos[0]).toEqual(todosBeforeDeleteComplet[1]); expect(todos[1]).toEqual(todosBeforeDeleteComplet[3]); From 0648e7a9015e014164d228ec5ef4dae4a58c5f5d Mon Sep 17 00:00:00 2001 From: Maharit Belai Date: Sun, 22 Nov 2026 12:03:05 +0000 Subject: [PATCH 06/11] changed the test name and removed the comment --- Sprint-3/todo-list/todos.test.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-3/todo-list/todos.test.mjs b/Sprint-3/todo-list/todos.test.mjs index f8042fee7e..117faf0647 100644 --- a/Sprint-3/todo-list/todos.test.mjs +++ b/Sprint-3/todo-list/todos.test.mjs @@ -128,7 +128,7 @@ test("should return the tasks that are not completed", () => { const todos = createMockTodos(); const todosBeforeDeleteComplet = createMockTodos(); const lengthBeforDeleteComplete = todos.length; - //Todos.deleteCompleted(todos); + Todos.deleteCompleted(todos); expect(todos).toHaveLength(lengthBeforDeleteComplete - 2); expect(todos[0]).toEqual(todosBeforeDeleteComplet[1]); expect(todos[1]).toEqual(todosBeforeDeleteComplet[3]); From 78338ecfb808b2a6c72782aca2ebe5e7ff2c0e4b Mon Sep 17 00:00:00 2001 From: Maharit Belai Date: Sun, 22 Nov 2026 12:07:38 +0000 Subject: [PATCH 07/11] removed the return array line because it only mutates the array no need to return --- Sprint-3/todo-list/todos.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/Sprint-3/todo-list/todos.mjs b/Sprint-3/todo-list/todos.mjs index 7d4b84baeb..a98e807c4b 100644 --- a/Sprint-3/todo-list/todos.mjs +++ b/Sprint-3/todo-list/todos.mjs @@ -34,5 +34,4 @@ export function deleteCompleted(todos) { i--; } } - return todos; } From 951bb0d771457b87724302598f6d9d59da572be7 Mon Sep 17 00:00:00 2001 From: Maharit Belai Date: Sun, 22 Nov 2026 12:07:57 +0000 Subject: [PATCH 08/11] changed the test name --- Sprint-3/todo-list/todos.test.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-3/todo-list/todos.test.mjs b/Sprint-3/todo-list/todos.test.mjs index 117faf0647..542ed39e51 100644 --- a/Sprint-3/todo-list/todos.test.mjs +++ b/Sprint-3/todo-list/todos.test.mjs @@ -124,7 +124,7 @@ describe("toggleCompletedOnTask()", () => { expect(todos).toEqual(todosBeforeToggle); }); }); -test("should return the tasks that are not completed", () => { +test("should remove completed tasks from the array", () => { const todos = createMockTodos(); const todosBeforeDeleteComplet = createMockTodos(); const lengthBeforDeleteComplete = todos.length; From 38f8bde15b1e899dfd630351b0d24f2a87e6436f Mon Sep 17 00:00:00 2001 From: Maharit Belai Date: Wed, 25 Nov 2026 18:26:31 +0000 Subject: [PATCH 09/11] wrote a test that return the same task when there is nothing to delet --- Sprint-3/todo-list/todos.test.mjs | 36 +++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/Sprint-3/todo-list/todos.test.mjs b/Sprint-3/todo-list/todos.test.mjs index 542ed39e51..be865a2b5a 100644 --- a/Sprint-3/todo-list/todos.test.mjs +++ b/Sprint-3/todo-list/todos.test.mjs @@ -35,7 +35,7 @@ describe("addTask()", () => { // todos should now have one more task expect(todos).toHaveLength(lengthBeforeAddition + 1); - // New task should be appended to the todos + //New task should be appended to the todos expect(todos[todos.length - 1]).toEqual(theTask); }); }); @@ -120,16 +120,40 @@ describe("toggleCompletedOnTask()", () => { Todos.toggleCompletedOnTask(todos, 10); expect(todos).toEqual(todosBeforeToggle); - // Todos.toggleCompletedOnTask(todos, -1); + Todos.toggleCompletedOnTask(todos, -1); expect(todos).toEqual(todosBeforeToggle); }); }); test("should remove completed tasks from the array", () => { const todos = createMockTodos(); - const todosBeforeDeleteComplet = createMockTodos(); + const todosBeforeDeleteComplete = createMockTodos(); const lengthBeforDeleteComplete = todos.length; Todos.deleteCompleted(todos); - expect(todos).toHaveLength(lengthBeforDeleteComplete - 2); - expect(todos[0]).toEqual(todosBeforeDeleteComplet[1]); - expect(todos[1]).toEqual(todosBeforeDeleteComplet[3]); + + const lengthAfterDeleteComplete = todos.length; + expect(todos).toHaveLength(lengthAfterDeleteComplete); + expect(todos[0]).toEqual(todosBeforeDeleteComplete[1]); + expect(todos[1]).toEqual(todosBeforeDeleteComplete[3]); + + // Tests for when there is no more tasks completed + const todosAfterFirstDeleteComplete = []; + todosAfterFirstDeleteComplete.push(...todos); + Todos.deleteCompleted(todos); + expect(todos).toHaveLength(lengthAfterDeleteComplete); + expect(todos[0]).toEqual(todosAfterFirstDeleteComplete[0]); + expect(todos[1]).toEqual(todosAfterFirstDeleteComplete[1]); +}); +test("should return the same tasks when there is no completed task", () => { + let todos = createMockTodos(); + todos = todos.filter((task) => { + !task.completed; + }); + const todosBeforeDeleteComplete = []; + todosBeforeDeleteComplete.push(...todos); + const LengthBeforeDeleteComplete = todos.length; + Todos.deleteCompleted(todos); + expect(todos).toEqual(todosBeforeDeleteComplete); + expect(todos).toHaveLength(LengthBeforeDeleteComplete); + expect(todos[0]).toEqual(todosBeforeDeleteComplete[0]); + expect(todos[1]).toEqual(todosBeforeDeleteComplete[1]); }); From 32fed107c21b152b299761c3881c9879d6168956 Mon Sep 17 00:00:00 2001 From: Maharit Belai Date: Wed, 25 Nov 2026 21:33:17 +0000 Subject: [PATCH 10/11] wrote a test to return empty array when all the task completed are deleted .test passed --- Sprint-3/todo-list/todos.test.mjs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Sprint-3/todo-list/todos.test.mjs b/Sprint-3/todo-list/todos.test.mjs index be865a2b5a..0312700f6f 100644 --- a/Sprint-3/todo-list/todos.test.mjs +++ b/Sprint-3/todo-list/todos.test.mjs @@ -134,26 +134,26 @@ test("should remove completed tasks from the array", () => { expect(todos).toHaveLength(lengthAfterDeleteComplete); expect(todos[0]).toEqual(todosBeforeDeleteComplete[1]); expect(todos[1]).toEqual(todosBeforeDeleteComplete[3]); - - // Tests for when there is no more tasks completed - const todosAfterFirstDeleteComplete = []; - todosAfterFirstDeleteComplete.push(...todos); - Todos.deleteCompleted(todos); - expect(todos).toHaveLength(lengthAfterDeleteComplete); - expect(todos[0]).toEqual(todosAfterFirstDeleteComplete[0]); - expect(todos[1]).toEqual(todosAfterFirstDeleteComplete[1]); }); test("should return the same tasks when there is no completed task", () => { let todos = createMockTodos(); - todos = todos.filter((task) => { - !task.completed; - }); + todos = todos.filter((task) => !task.completed); const todosBeforeDeleteComplete = []; todosBeforeDeleteComplete.push(...todos); - const LengthBeforeDeleteComplete = todos.length; + const lengthBeforeDeleteComplete = todos.length; Todos.deleteCompleted(todos); expect(todos).toEqual(todosBeforeDeleteComplete); - expect(todos).toHaveLength(LengthBeforeDeleteComplete); + expect(todos).toHaveLength(lengthBeforeDeleteComplete); expect(todos[0]).toEqual(todosBeforeDeleteComplete[0]); expect(todos[1]).toEqual(todosBeforeDeleteComplete[1]); }); +test("should return empty array when all the task completed are deleted", () => { + let todos = createMockTodos(); + todos = todos.filter((task) => task.completed); + const todosBeforeDeleteComplete = []; + todosBeforeDeleteComplete.push(...todos); + const LengthBeforeDeleteComplete = todos.length; + Todos.deleteCompleted(todos); + expect(todos).toEqual([]); + expect(todos).toHaveLength(0); +}); From 454b88829982f8fbe1771349eb53797a429ef308 Mon Sep 17 00:00:00 2001 From: Maharit Belai Date: Wed, 25 Nov 2026 22:41:36 +0000 Subject: [PATCH 11/11] edited the deletCompleted function as advised --- Sprint-3/todo-list/todos.mjs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Sprint-3/todo-list/todos.mjs b/Sprint-3/todo-list/todos.mjs index a98e807c4b..9e7a38c7b8 100644 --- a/Sprint-3/todo-list/todos.mjs +++ b/Sprint-3/todo-list/todos.mjs @@ -28,10 +28,6 @@ export function toggleCompletedOnTask(todos, taskIndex) { } } export function deleteCompleted(todos) { - for (let i = 0; i < todos.length; i++) { - if (todos[i].completed) { - todos.splice(i, 1); - i--; - } - } + const notCompleted = todos.filter((task) => !task.completed); + todos.splice(0, todos.length, ...notCompleted); }