forked from Codi-T02/Node-Basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.js
156 lines (131 loc) · 3.21 KB
/
tasks.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Starts the application
* This is the function that is run when the app starts
*
* It prints a welcome line, and then a line with "----",
* then nothing.
*
* @param {string} name the name of the app
* @returns {void}
*/
function startApp(name){
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', onDataReceived);
console.log(`Welcome to ${name}'s application!`)
console.log("--------------------")
}
/**
* Decides what to do depending on the data that was received
* This function receives the input sent by the user.
*
* For example, if the user entered
* ```
* node tasks.js batata
* ```
*
* The text received would be "batata"
* This function then directs to other functions
*
* @param {string} text data typed by the user
* @returns {void}
*/
function onDataReceived(text) {
if (text === 'quit\n' || text === 'exit\n') {
quit();
}
else if (text === 'list\n'){
list();
}
else if(text.trim().split(" ")[0]==="hello") {
hello(text.trim().substring(5));
}
else if (text.trim().split(" ")[0]==="add") {
add(text.trim().substring(4));
}
else if (text.trim().split(" ")[0] === "remove") {
remove(text.trim().substring(6));
}
else if(text==='help\n') {
help();
}
else{
unknownCommand(text);
}
}
/**
* prints "unknown command"
* This function is supposed to run when all other commands have failed
*
* @param {string} c the text received
* @returns {void}
*/
function unknownCommand(c){
console.log('unknown command: "'+c.trim()+'"')
}
/**
* Says hello
*
* @returns {void}
*/
function hello(text){
console.log(`hello${text}!`)
}
/**
* Exits the application
*
* @returns {void}
*/
function quit(){
console.log('Quitting now, goodbye!')
process.exit();
}
/**
* prints
* "
* command desciption
* .................................
* hello greeting user.
* quit OR exit end the application.
* help show command.
* ..................................
* "
*
* Help command used to get an idea of the script you are running
*
* @returns {void}
*/
function help(){
console.log(`
command\t\tdescription
....................................
hello\t\t\tuser greeting.
\t\t\t\t(usage)- hello "name"
quit or exit\t\tend the application.
help\t\t\tshow script commands.
list\t\t\tlist command used to show the list of tasks.
add\t\t\t\tadd command used to add task to the tasks list.
\t\t\t\t(usage)- add "taskname"
remove\t\t\tremove command used to remove a task
\t\t\t\tfrom the tasks list by the number of the task.
\t\t\t\t(usage)- remove "number of task"
....................................
`)
}
var List = Array("task1","task2");
function list() {
console.log(
List.map((element,key)=>`${key+1} - ${element}`).join('\n')
)
}
function add(text) {
if(text.length==0){ console.log("you didn't input any data");return;}
List.push(text)
}
function remove(index) {
if(index.length==0){List.pop(); return;}
if(Number(index) >=1 && Number(index) <=List.length) {List.splice(index-1, 1);return;}
console.log(" type a valid number ")
}
// The following line starts the application
startApp("Ahmad Sabaa")