-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.ts
91 lines (75 loc) · 2.13 KB
/
todo.ts
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// A class to represent a todo item
class Todo {
id: number;
text: string;
completed: boolean;
constructor(id: number, text: string, completed: boolean) {
this.id = id;
this.text = text;
this.completed = completed;
}
}
// A class to represent a todo list
class TodoList {
todos: Todo[];
nextId: number;
constructor() {
this.todos = [];
this.nextId = 1;
}
// A method to add a new todo item
addTodo(text: string) {
const todo = new Todo(this.nextId, text, false);
this.todos.push(todo);
this.nextId++;
}
// A method to toggle the completion status of a todo item by id
toggleTodo(id: number) {
const todo = this.todos.find((t) => t.id === id);
if (todo) {
todo.completed = !todo.completed;
}
}
// A method to remove a todo item by id
removeTodo(id: number) {
const index = this.todos.findIndex((t) => t.id === id);
if (index !== -1) {
this.todos.splice(index, 1);
}
}
// A method to display the todo list on the console
displayTodos() {
console.log("Todo List:");
for (const todo of this.todos) {
console.log(
`${todo.id}. ${todo.text} ${
todo.completed ? "(completed)" : "(not completed)"
}`
);
}
}
}
// A function to test the todo list functionality
function testTodoList() {
// Create a new todo list
const todoList = new TodoList();
// Add some todos
todoList.addTodo("Learn typescript");
todoList.addTodo("Create a todo list app");
todoList.addTodo("Have fun");
// Display the todos
todoList.displayTodos();
// Toggle the completion status of the first and third todos
todoList.toggleTodo(1);
todoList.toggleTodo(3);
// Display the updated todos
console.log("\nAfter toggling:");
todoList.displayTodos();
// Remove the second todo
todoList.removeTodo(2);
// Display the final todos
console.log("\nAfter removing:");
todoList.displayTodos();
}
// Run the test function
testTodoList();