Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
924fde0
Added a button id for deletedcompleted job in the index .html file
Tobias-Amaechina Jul 28, 2026
3c084ca
implement the function deleteCompleted to handle the deletion of mas…
Tobias-Amaechina Jul 28, 2026
9455547
Add a click listener to the window button in script.mjs file
Tobias-Amaechina Jul 28, 2026
ff96587
Declare the function to handle deletion of completed task
Tobias-Amaechina Jul 28, 2026
ee44b03
Install the package.json
Tobias-Amaechina Jul 28, 2026
9e82873
Wrote a test to check that the implemention deletedcompleted task p…
Tobias-Amaechina Jul 28, 2026
56d0053
update the template to have date picker Id
Tobias-Amaechina Jul 28, 2026
902445b
extended the addtask function to include deadline
Tobias-Amaechina Jul 28, 2026
60f6e05
update the addNewTodo() to include deadline when creating task
Tobias-Amaechina Jul 28, 2026
03d89a6
Updated sample data to handle deadline
Tobias-Amaechina Jul 28, 2026
074dd2c
Added a span for deadline in the template file
Tobias-Amaechina Jul 28, 2026
251083e
set deadline text in the script.mjs file
Tobias-Amaechina Jul 28, 2026
fd329c5
Add helper function to show the remaining days
Tobias-Amaechina Jul 28, 2026
85a63c8
Updated list item of deadline to handle remaing days
Tobias-Amaechina Jul 28, 2026
71aac32
Wrote a test to pass Deadline feature
Tobias-Amaechina Jul 28, 2026
fe7adc4
Merge branch 'main' into group-data-sprint-todo
Tobias-Amaechina Jul 28, 2026
30cdff5
formatt the index.html to have good identation
Tobias-Amaechina Aug 3, 2026
d971640
Updated deleCompleted function so it modifies the passed array
Tobias-Amaechina Aug 3, 2026
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
14 changes: 8 additions & 6 deletions Sprint-3/todo-list/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ToDo List</title>
<link rel="stylesheet" href="style.css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet" />

<script type="module" src="script.mjs"></script>
</head>
Expand All @@ -15,11 +15,13 @@ <h1>My ToDo List</h1>

<div class="todo-input">
<input type="text" id="new-task-input" placeholder="Enter a new task..." />
<input type="date" id="new-task-deadline" />
<button id="add-task-btn">Add</button>
</div>

<ul id="todo-list" class="todo-list">
</ul>
<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 All @@ -28,13 +30,13 @@ <h1>My ToDo List</h1>
<template id="todo-item-template">
<li class="todo-item"> <!-- include class "completed" if the task completed state is true -->
<span class="description">Task description</span>
<span class="deadline"></span>
<div class="actions">
<button class="complete-btn"><span class="fa-solid fa-check" aria-hidden="true"></span></button>
<button class="delete-btn"><span class="fa-solid fa-trash" aria-hidden="true"></span></button>
</div>
</li>
</template>

</div>
</body>
</html>
</html>
2 changes: 1 addition & 1 deletion Sprint-3/todo-list/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"devDependencies": {
"http-server": "^14.1.1",
"jest": "^30.0.4"
"jest": "^30.4.2"
}
}
54 changes: 51 additions & 3 deletions Sprint-3/todo-list/script.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ const todos = [];
// Set up tasks to be performed once on page load
window.addEventListener("load", () => {
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);
document.getElementById("delete-completed-btn")
.addEventListener("click", deleteCompletedTodos);


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


render();
});
Expand All @@ -20,15 +24,30 @@ window.addEventListener("load", () => {
// append a new task to the todo list.
function addNewTodo() {
const taskInput = document.getElementById("new-task-input");
const deadlineInput = document.getElementById("new-task-deadline");

const task = taskInput.value.trim();
const deadline = deadlineInput.value || null;

if (task) {
Todos.addTask(todos, task, false);
Todos.addTask(todos, task, false, deadline);
render();
}

taskInput.value = "";
deadlineInput.value = "";
}

function deleteCompletedTodos() {
const newTodos = Todos.deleteCompleted(todos);

// Replace the old array contents
Todos.deleteCompleted(todos);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not perform the operations on lines 45-46 in Todos.deleteCompleted()?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the feedback , the operation has been updated now

render();
}


// Note:
// - Store the reference to the <ul> element with id "todo-list" here
// to avoid querying the DOM repeatedly inside render().
Expand All @@ -52,6 +71,28 @@ function render() {
// - This variable is declared here to be close to the only function that uses it.
const todoListItemTemplate =
document.getElementById("todo-item-template").content.firstElementChild;
function getDaysRemainingText(deadline) {
if (!deadline) return "";

const today = new Date();
today.setHours(0, 0, 0, 0);

const deadlineDate = new Date(deadline);
deadlineDate.setHours(0, 0, 0, 0);

const msPerDay = 1000 * 60 * 60 * 24;
const diffDays = Math.round((deadlineDate - today) / msPerDay);

if (diffDays === 0) {
return "Due today";
} else if (diffDays > 0) {
return `${diffDays} day${diffDays === 1 ? "" : "s"} left`;
} else {
const overdueDays = Math.abs(diffDays);
return `Overdue by ${overdueDays} day${overdueDays === 1 ? "" : "s"}`;
}
}


// Create a <li> element for the given todo task
function createListItem(todo, index) {
Expand All @@ -61,6 +102,13 @@ function createListItem(todo, index) {
if (todo.completed) {
li.classList.add("completed");
}
const deadlineEl = li.querySelector(".deadline");
if (todo.deadline) {
deadlineEl.textContent = getDaysRemainingText(todo.deadline);
} else {
deadlineEl.textContent = "";
}


li.querySelector('.complete-btn').addEventListener("click", () => {
Todos.toggleCompletedOnTask(todos, index);
Expand Down
15 changes: 12 additions & 3 deletions Sprint-3/todo-list/todos.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
*/

// 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) {
todos.push({ task, completed, deadline });
}


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

export function deleteCompleted(todoList) {
const remaining = todoList.filter((todo) => !todo.completed);
todoList.length = 0;
todoList.push(...remaining);

return todoList;
}
20 changes: 16 additions & 4 deletions Sprint-3/todo-list/todos.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ function createMockTodos() {
}

// 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", () => {
let todos = [];
Todos.addTask(todos, theTask.task, theTask.completed);
expect(todos).toHaveLength(1);
expect(todos[0]).toEqual(theTask);
Todos.addTask(todos, theTask.task, theTask.completed);
expect(todos[todos.length - 1]).toEqual(theTask);
});

test("Should append a new task to the end of a ToDo list", () => {
Expand Down Expand Up @@ -130,3 +129,16 @@ describe("toggleCompletedOnTask()", () => {
});
});



test("deleteCompleted removes all completed tasks", () => {
const todos = [
{ task: "A", completed: true },
{ task: "B", completed: false },
{ task: "C", completed: true },
];

const result = Todos.deleteCompleted(todos);

expect(result).toEqual([{ task: "B", completed: false }]);
});
Loading