-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
69 lines (57 loc) · 1.83 KB
/
todo.js
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
import chalk from 'chalk';
import readline from 'readline';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let todoList = [];
let todolist = [];
const displayTodo = () => {
console.log(chalk.blue('\nTo-Do List:'));
if (todolist.length === 0){
console.log(chalk.yellow('No tasks in the list.'));
}
else{
todolist.forEach((task, index) => {
console.log(chalk.cyan(`${index +1}: `) + chalk.white.bold(task));
});
}
};
const promptUser = () => {
rl.question('\nChoose an action (add("a")/view("v")/remove("r")/exit("e")): ',(action) => {
if (action === 'a'){
rl.question('Enter a task to add: ', (task) => {
todolist.push(task);
console.log(chalk.green(`Added: "${task}"`));
displayTodo();
promptUser();
});
}
else if (action === 'v'){
displayTodo();
promptUser();
}
else if(action === 'r'){
rl.question('Enter the task number to remove: ', (index) => {
const remtask = todolist.splice(index - 1, 1);
if (remtask.length > 0){
console.log(chalk.red(`Removed: "${remtask[0]}"`));
}
else{
console.log(chalk.red('Invalid task number.'));
}
displayTodo();
promptUser();
});
}
else if (action === 'e'){
console.log(chalk.blue.bold('Bye bye!'));
}
else{
console.log(chalk.red('Invalid action. Please choose (add("a")/view("v")/remove("r")/exit("e")).'));
promptUser();
}
});
};
console.log(chalk.green.bold("Welcome to the To-Do List App!"));
promptUser();