-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.js
More file actions
257 lines (205 loc) · 5.46 KB
/
tasks.js
File metadata and controls
257 lines (205 loc) · 5.46 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var fs = require('fs');
const fileName = process.argv[2] || 'tasks.json';
var fileContents
if (fs.existsSync(fileName)) {
fileContents = fs.readFileSync(fileName, 'utf8');
} else {
fs.writeFileSync(fileName, "[]")
fileContents = fs.readFileSync(fileName, 'utf8');
}
const tasks = JSON.parse(fileContents);
const { title } = require('process');
const { finished } = require('stream');
const { isUndefined } = require('util');
/**
* 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) {
text = text.replace("\n", " ").trim().split(" ");
// const first = text[0];
if (text[0] === 'quit'|| text[0] === 'exit') {
quit();
}
else if(text[0] === 'hello'){
hello(text);
}
else if(text[0] === 'help'){
help();
}
else if(text[0] === 'list'){
list()
}
else if(text[0] === 'add'){
text.shift();
let x=text.join(" ");
add(x, false);
}
else if(text[0] === 'remove'){
remove(text);
}
else if(text[0] === 'edit' || text[0] === 'Edit'){
let i= text[1]
text.shift()
text.shift()
let newText = text.join(" ");
edit(i, newText);
}
else if(text[0] === 'check'){
text.shift();
text.join(" ");
check(text);
}
else if(text[0] === 'uncheck'){
text.shift();
unCheck(text);
}
else{
unknownCommand(text[0]);
}
}
/**
* 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() +'"')
}
function hello(x){
if(x[1]===undefined){
console.log("hello!");}
else {
console.log('hello ' + x[1] + '!')}
}
// // var tasks =
// var tasks =[{ task : "woke up " ,done : false},
// { task : "open laptop " ,done : false},{ task : "open VS code " ,done : false},{ task : "work on your assignment" ,done : false}]
function list(){
for(let i= 0; i<tasks.length ; i++) {
const taski =tasks[i]
let tasksCheck = "[ ]";
if (taski.done){
tasksCheck = "[✓]";
}
console.log(`${i + 1}:${tasksCheck} ${taski.task} `);
};
}
//add function
function add(task ,done){
if (!task && !done) {
console.log('Error! there is No task added ');
} else {
tasks.push({task ,done});
console.log(`Added ${task} to the list`);
var data = JSON.stringify(tasks, null, 2);
fs.writeFileSync(fileName, data, finished);
}
}
//remove function
function remove(x) {
if (x == 'remove' ) {
let num=tasks.pop();
console.log(num);
}
else if(x[1] === "1"){
let num1=tasks.slice(0,1);
console.log(num1);
}
else if(x[1] === '2'){
let num2=tasks.slice(1,2);
console.log(num2);
}
else if(x[1] === '3'){
let num3=tasks.slice(2,3);
console.log(num3);
}
else if(x[1] === '4'){
let num4=tasks.slice(3,4);
console.log(num4);
}
else if(x[1] === '5'){
let num5=tasks.slice(4,5);
console.log(num5);
}
else{
console.log(tasks);
}
}
//edit function
function edit(i, newText){
if(!i && !newText){
console.log('Error: add the new text')
}
else if (i >= 0 && i < tasks.length) {
tasks[i-1].task = newText
}
}
//check function
function check(index) {
if (!index) {
console.log("Error: no task index provided");
}else{
tasks[index-1].done = true;
var data = JSON.stringify(tasks, null, 2);
fs.writeFileSync(fileName, data, finished);
}
}
function unCheck(index) {
if (!index ) {
console.log("Error: no task index provided");
}else{
tasks[index-1].done = false;
var data = JSON.stringify(tasks, null, 2);
fs.writeFileSync(fileName, data, finished);
}
}
/**
* help Function => it lists all the possible commands
*
* @returns {void}
*/
function help(){
console.log('\n-hello : hello!'+'\n-hello x : hello x! =>this command put "!"after the string containing two word'+'\n-quit or exit => it exit from the program'+'\n-help => help command ,this command lists the possible command in program'+'\n-list => it list all the task'+'\n-add => it add task to the list'+'\n-remove => remove the last element from the list '+ '\n-remove 1 => it remove the first item in the list'+'\n-remove 2 => it remove the second item in the list'+ '\n-remove 3 => it remove the third item in the list'+ '\n-edit x newText => this command edit the task at specific index'+'\n check 1 => it will check the task at id :1'+'\n-uncheck 1 =>this command uncheck the task at id :1 ')
}
/**
* Exits the application
*
* @returns {void}
*/
function quit(){
console.log('Quitting now, goodbye!')
process.exit();
}
// The following line starts the application
startApp("Bodour Almalki")