Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sprint-3/todo-list/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ <h1>My ToDo List</h1>

<ul id="todo-list" class="todo-list">
</ul>
<button id="delete-completed-btn">Delete completed tasks</button>

<!--
This is a template for the To-do list item.
Expand Down
13 changes: 12 additions & 1 deletion Sprint-3/todo-list/script.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ const todos = [];
window.addEventListener("load", () => {
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);

document
.getElementById("delete-completed-btn")
.addEventListener("click", deleteCompletedTasks);

// Populate sample data
Todos.addTask(todos, "Wash the dishes", false);
Todos.addTask(todos, "Do the shopping", true);

Todos.addTask(todos, "Finish assignment", false, "2026-04-01");

render();
});

Expand Down Expand Up @@ -73,4 +79,9 @@ function createListItem(todo, index) {
});

return li;
}
}

function deleteCompletedTasks() {
Todos.deleteCompleted(todos);
render();
}
16 changes: 13 additions & 3 deletions Sprint-3/todo-list/todos.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/

// Append a new task to todos[]
export function addTask(todos, task, completed = false) {
todos.push({ task, completed });
export function addTask(todos, task, completed = false, deadline = null) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can a user choose a deadline for the task?

Copy link
Copy Markdown
Author

@carlosyabreu carlosyabreu Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If addTask function had a deadline parameter but the function body doesn't actually use it (it still only creates { task, completed }), there are a few ways a user could choose a deadline:

Modify the function to include deadline in the task object
First, it needs to update the function implementation to actually store the deadline:

// Updated todos.mjs
export function addTask(todos, task, completed = false, deadline = null) {
todos.push({ task, completed, deadline });
}

Then a user could specify a deadline when calling the function:

// Pass deadline as the 4th parameter
const deadline = new Date("2026-12-31");
Todos.addTask(todos, "Finish project", false, deadline);

// Or with different deadlines
Todos.addTask(todos, "Morning meeting", false, new Date("2026-04-07T09:00:00"));
Todos.addTask(todos, "Submit report", false, new Date("2026-04-10"));

todos.push({ task, completed, deadline });
}

// Delete todos[taskIndex] if it exists
Expand All @@ -26,4 +26,14 @@ export function toggleCompletedOnTask(todos, taskIndex) {
if (todos[taskIndex]) {
todos[taskIndex].completed = !todos[taskIndex].completed;
}
}
}

// Remove all completed tasks from the todos array
export function deleteCompleted(todos) {
// Iterate backwards to safely remove items while mutating array
for (let i = todos.length - 1; i >= 0; i--) {
if (todos[i].completed) {
todos.splice(i, 1);
}
}
Comment on lines +34 to +38
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works correctly. You can also research build in array functions that allow you to filter elements

Copy link
Copy Markdown
Author

@carlosyabreu carlosyabreu Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing that out.
Definitely I'll research that option of building an array functions that allows element filtering to expand my knowledge.

}
51 changes: 46 additions & 5 deletions Sprint-3/todo-list/todos.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import * as Todos from "./todos.mjs";
// Return a mock ToDo List data with exactly 4 elements.
function createMockTodos() {
return [
{ 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 1 description", completed: true, deadline: "2026-04-01" },
{ task: "Task 2 description", completed: false, deadline: null },
{ task: "Task 3 description", completed: true, deadline: "2026-04-05" },
{ task: "Task 4 description", completed: false, deadline: null },
];
}

// A mock task to simulate user input
const theTask = { task: "The Task", completed: false };
const theTask = { task: "The Task", completed: false, deadline: null };

describe("addTask()", () => {
test("Add a task to an empty ToDo list", () => {
Expand Down Expand Up @@ -130,3 +130,44 @@ describe("toggleCompletedOnTask()", () => {
});
});

describe("deleteCompleted()", () => {

test("Remove all completed tasks", () => {
const todos = createMockTodos();

Todos.deleteCompleted(todos);

// Only incomplete tasks should remain
expect(todos).toHaveLength(2);
expect(todos[0].completed).toBe(false);
expect(todos[1].completed).toBe(false);

expect(todos[0].task).toBe("Task 2 description");
expect(todos[1].task).toBe("Task 4 description");
});

test("No change if no tasks are completed", () => {
const todos = [
{ task: "Task A", completed: false },
{ task: "Task B", completed: false }
];

const before = JSON.parse(JSON.stringify(todos));

Todos.deleteCompleted(todos);

expect(todos).toEqual(before);
});

test("All tasks removed if all are completed", () => {
const todos = [
{ task: "Task A", completed: true },
{ task: "Task B", completed: true }
];

Todos.deleteCompleted(todos);

expect(todos).toHaveLength(0);
});

});
Loading