Skip to content

Commit 20ce01c

Browse files
committed
update implementation index.html and script.js with delete button
1 parent 96d077b commit 20ce01c

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

Sprint-3/todo-list/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ <h1>My ToDo List</h1>
2020

2121
<ul id="todo-list" class="todo-list">
2222
</ul>
23-
23+
<button id="delete-completed-btn">
24+
<!-- update styling -->
25+
Delete completed tasks
26+
</button>
2427
<!--
2528
This is a template for the To-do list item.
2629
It can simplify the creation of list item node in JS script.

Sprint-3/todo-list/script.mjs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1-
// Store everything imported from './todos.mjs' module as properties of an object named Todos
1+
// Store everything imported from './todos.mjs' module as properties of an object named Todos
2+
23
import * as Todos from "./todos.mjs";
34

45
// To store the todo tasks
56
const todos = [];
67

78
// Set up tasks to be performed once on page load
89
window.addEventListener("load", () => {
10+
//get the deleted complete button from html
11+
const massDeleteBtn = document.querySelector("#delete-completed-btn");
12+
13+
// check if the button is even found in the HTML
14+
// console.log("Button found:", massDeleteBtn);
15+
16+
if (massDeleteBtn) {
17+
massDeleteBtn.addEventListener("click", () => {
18+
// check if the click event is firing
19+
// console.log("Mass delete button clicked!");
20+
// console.log("Todos before delete:", JSON.parse(JSON.stringify(todos)));
21+
Todos.deleteCompleted(todos);
22+
// check if the array actually changed
23+
// console.log("Todos after delete:", JSON.parse(JSON.stringify(todos)));
24+
render();
25+
});
26+
}
27+
928
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);
1029

1130
// Populate sample data
12-
Todos.addTask(todos, "Wash the dishes", false);
31+
Todos.addTask(todos, "Wash the dishes", false);
1332
Todos.addTask(todos, "Do the shopping", true);
1433

1534
render();
1635
});
1736

1837

19-
// A callback that reads the task description from an input field and
38+
// A callback that reads the task description from an input field and
2039
// append a new task to the todo list.
2140
function addNewTodo() {
2241
const taskInput = document.getElementById("new-task-input");
@@ -31,7 +50,7 @@ function addNewTodo() {
3150

3251
// Note:
3352
// - Store the reference to the <ul> element with id "todo-list" here
34-
// to avoid querying the DOM repeatedly inside render().
53+
// to avoid querying the DOM repeatedly inside render().
3554
// - This variable is declared here to be close to the only function that uses it.
3655
const todoListEl = document.getElementById("todo-list");
3756

@@ -48,9 +67,9 @@ function render() {
4867

4968
// Note:
5069
// - First child of #todo-item-template is a <li> element.
51-
// We will create each ToDo list item as a clone of this node.
70+
// We will create each ToDo list item as a clone of this node.
5271
// - This variable is declared here to be close to the only function that uses it.
53-
const todoListItemTemplate =
72+
const todoListItemTemplate =
5473
document.getElementById("todo-item-template").content.firstElementChild;
5574

5675
// Create a <li> element for the given todo task
@@ -62,15 +81,15 @@ function createListItem(todo, index) {
6281
li.classList.add("completed");
6382
}
6483

65-
li.querySelector('.complete-btn').addEventListener("click", () => {
84+
li.querySelector(".complete-btn").addEventListener("click", () => {
6685
Todos.toggleCompletedOnTask(todos, index);
6786
render();
6887
});
69-
70-
li.querySelector('.delete-btn').addEventListener("click", () => {
88+
89+
li.querySelector(".delete-btn").addEventListener("click", () => {
7190
Todos.deleteTask(todos, index);
7291
render();
7392
});
7493

7594
return li;
76-
}
95+
}

Sprint-3/todo-list/todos.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ export function toggleCompletedOnTask(todos, taskIndex) {
2626
if (todos[taskIndex]) {
2727
todos[taskIndex].completed = !todos[taskIndex].completed;
2828
}
29+
}
30+
31+
// Removes all completed ToDos from the given list
32+
export function deleteCompleted(todos) {
33+
// We iterate backwards to avoid index shifting issues during mutation
34+
for (let i = todos.length - 1; i >= 0; i--) {
35+
if (todos[i].completed) {
36+
todos.splice(i, 1);
37+
}
38+
}
2939
}

Sprint-3/todo-list/todos.test.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,21 @@ describe("toggleCompletedOnTask()", () => {
130130
});
131131
});
132132

133+
describe("deleteCompleted()", () => {
134+
test("should remove all completed tasks and keep only active ones", () => {
135+
// 1. Setup a list with mixed states
136+
const todos = [
137+
{ task: "Task A", completed: true },
138+
{ task: "Task B", completed: false },
139+
{ task: "Task C", completed: true }
140+
];
141+
142+
// 2. Execute the function
143+
Todos.deleteCompleted(todos);
144+
145+
// 3. Verify: only Task B should remain
146+
expect(todos).toHaveLength(1);
147+
expect(todos[0].task).toBe("Task B");
148+
expect(todos[0].completed).toBe(false);
149+
});
150+
});

0 commit comments

Comments
 (0)