Write a class named TodoList
that is used to create objects that behave like a real-life todo list. Each object should have the following methods:
add
: a function that adds a new todo item to the list. It receives 2 parameters:
id
task
to be doneIf there is already a todo item with this id
, the function should throw an error with the message "There is already a todo item with this id.".
remove
: a function that removes a todo item with a certain id from the list. It receives 1 parameter:
id
The function shouldn't throw an error if there's no todo item with the given id
.
markAsDone
: a function that marks a todo item as done. It receives 1 parameter:
id
If there's no todo item with the given id
, the function should throw an error with the message "There is no todo item with this id.".
getItem
: a function that returns a todo item. It receives 1 parameter:
id
If there's no todo item with the given id
, the function should return null
;
getAll
: a function that returns an Array of todo items. Each item in the array should have 3 properties:
id
: the id of the todo itemtask
: a description of the task to be donedone
: a boolean that indicates if the todo item is done or notThe items in the array should be sorted ascending, by the id
property.
const todoList = new TodoList();
todoList.add("groceries", "Buy groceries");
todoList.add("kids", "Pick up kids from school");
console.log(todoList.getItem("groceries"));
// Should print: { id: "groceries", task: "Buy groceries", done: false }
console.log(todoList.getItem("kids"));
// Should print: { id: "kids", task: "Pick up kids from school", done: false }
todoList.markAsDone("groceries");
console.log(todoList.getItem("groceries"));
// Should print: { id: "groceries", task: "Buy groceries", done: true }
todoList.getAll();
/**
* Should print:
* [
* { id: "groceries", task: "Buy groceries", done: true },
* { id: "kids", task: "Pick up kids from school", done: false }
* ]
*/