Elffuss BraidHub

kikocis/continue public

main · 2.2 KiBRawPermalink
1// A poorly structured todo list manager
2// Difficulty: 2 - Multiple issues to fix
3
4// Global variables
5let todos: {
6 id: number;
7 text: string;
8 completed: boolean;
9 priority: string;
10}[] = [];
11let lastId = 0;
12
13// Add a new todo
14function addTodo(text: string, priority = "medium") {
15 const newTodo = {
16 id: ++lastId,
17 text: text,
18 completed: false,
19 priority: priority,
20 };
21 todos.push(newTodo);
22 return newTodo;
23}
24
25// Delete a todo by id
26function deleteTodo(id: number) {
27 for (let i = 0; i < todos.length; i++) {
28 if (todos[i].id === id) {
29 todos.splice(i, 1);
30 return true;
31 }
32 }
33 return false;
34}
35
36// Toggle todo completion status
37function toggleTodo(id: number) {
38 for (let i = 0; i < todos.length; i++) {
39 const todo = todos[i];
40 if (todo.id === id) {
41 todo.completed = !todo.completed;
42 return todo;
43 }
44 }
45 return null;
46}
47
48// Update todo text
49function updateTodoText(id: number, newText: string) {
50 for (let i = 0; i < todos.length; i++) {
51 if (todos[i].id === id) {
52 todos[i].text = newText;
53 return todos[i];
54 }
55 }
56 return null;
57}
58
59// Change todo priority
60function changePriority(id: number, newPriority: string) {
61 for (let i = 0; i < todos.length; i++) {
62 if (todos[i].id === id) {
63 todos[i].priority = newPriority;
64 return todos[i];
65 }
66 }
67 return null;
68}
69
70// Filter todos by status
71function filterByStatus(completed: boolean) {
72 const result = [];
73 for (let i = 0; i < todos.length; i++) {
74 if (todos[i].completed === completed) {
75 result.push(todos[i]);
76 }
77 }
78 return result;
79}
80
81// Filter todos by priority
82function filterByPriority(priority: string) {
83 const result = [];
84 for (let i = 0; i < todos.length; i++) {
85 if (todos[i].priority === priority) {
86 result.push(todos[i]);
87 }
88 }
89 return result;
90}
91
92// Example usage
93addTodo("Buy groceries", "high");
94addTodo("Clean house");
95addTodo("Pay bills", "high");
96toggleTodo(2);
97
98console.log("All todos:", todos);
99console.log("High priority todos:", filterByPriority("high"));
100console.log("Completed todos:", filterByStatus(true));
101
102export {
103 addTodo,
104 changePriority,
105 deleteTodo,
106 filterByPriority,
107 filterByStatus,
108 todos,
109 toggleTodo,
110 updateTodoText,
111};