-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp-43.js
More file actions
92 lines (79 loc) Β· 1.86 KB
/
p-43.js
File metadata and controls
92 lines (79 loc) Β· 1.86 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
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
92
// ! Write to-do list program that allows user to add, remove, and list tasks.
// ??? First install "npm install prompt-sync"
const prompt = require("prompt-sync")();
let running = true;
let tasks = [];
function addTask() {
let userTask = prompt("Enter a task: ").trim();
if (!userTask) {
console.log("β οΈ Task cannot be empty!\n");
return;
}
tasks.push(userTask);
console.log("β
Task Added!\n");
}
function checkTask() {
if (tasks.length === 0) {
console.log("No tasks yet!\n");
return;
}
console.log("\nYour Tasks:");
tasks.forEach((task, index) => {
console.log(`${index + 1}. ${task}`);
});
console.log();
}
function removeTask() {
if (tasks.length === 0) {
console.log("No tasks to remove.\n");
return;
}
console.log("\nYour Tasks:");
tasks.forEach((task, index) => {
console.log(`${index + 1}. ${task}`);
});
console.log();
let toRemove = prompt("Enter the task number to remove: ");
toRemove = parseInt(toRemove, 10);
if (isNaN(toRemove) || toRemove < 1 || toRemove > tasks.length) {
console.log("Invalid choice!\n");
return;
}
tasks.splice(toRemove - 1, 1);
console.log("Task Removed!\n");
}
function todoApp() {
while (running) {
let choice = prompt(
`---- Options ----- \n
1. Add Task
2. Check All Tasks
3. Remove a Task
4. Exit
Choose Your Option: `
);
if (!choice) {
console.log("Please enter a number!\n");
continue; // go back to loop
}
choice = parseInt(choice, 10);
switch (choice) {
case 1:
addTask();
break;
case 2:
checkTask();
break;
case 3:
removeTask();
break;
case 4:
console.log("π Come again! Bye bye");
running = false;
break;
default:
console.log("Invalid option. Please choose 1β4.\n");
}
}
}
todoApp();