-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathtodos.mjs
More file actions
39 lines (33 loc) · 1.02 KB
/
todos.mjs
File metadata and controls
39 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
A ToDo List (todos) is expected to be represented as an array of objects in
the following manner:
[
{ task: "Description of task 1", completed: false},
{ task: "Description of task 2", completed: true}
]
*/
// Append a new task to todos[]
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]) {
todos.splice(taskIndex, 1);
}
}
// Toggle the "completed" property of todos[taskIndex] if the task exists.
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);
}
}
}