Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 987f8bc

Browse files
NodeJS Week 2 homework is done.
1 parent b37cba8 commit 987f8bc

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
const help = [
4+
{ "T0-DO LIST HELP MENU": "USEFUL COMMANDS" },
5+
{
6+
"• HELP MENU ":
7+
'Open the help menu with "node index" or "node index help"'
8+
},
9+
{ "• SHOW LIST ": 'See the list of to-dos with "node index list"' },
10+
{ "• ADD ITEM ": 'Add a to-do item with "node index add "itemname""' },
11+
{
12+
"• UPDATE ITEM ": 'Update with "node index update "itemNR""newvalue"'
13+
},
14+
{
15+
"• REMOVE ITEM ": 'Remove a to-do item with "node index remove itemNR"'
16+
},
17+
{ "• RESET ": 'Reset the list with "node index reset"' }
18+
];
19+
20+
module.exports = { help };
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
5+
const utilityRead = function() {
6+
fs.readFile("./todolist.json", "utf8", (err, data) => {
7+
if (err) {
8+
console.log("TO DO LIST:");
9+
console.log("[]");
10+
} else {
11+
try {
12+
let showcase = JSON.parse(data);
13+
let index = [];
14+
for (let i = 0; i < showcase.length; i++) {
15+
index.push("Mission: " + (i + 1) + ":", showcase[i]);
16+
}
17+
console.log("TO DO LIST:");
18+
console.log(index);
19+
} catch {
20+
console.log("TO DO LIST:");
21+
console.log("[]");
22+
}
23+
}
24+
});
25+
};
26+
27+
module.exports = { utilityRead };
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
5+
const utilityRemove = function(index) {
6+
try {
7+
let currentData = fs.readFileSync("./todolist.json");
8+
9+
let showcase = JSON.parse(currentData);
10+
11+
let toDoList = [];
12+
for (let i = 0; i < showcase.length; i++) {
13+
toDoList.push(showcase[i]);
14+
}
15+
16+
if (toDoList.length > 0 && toDoList.length + 1 > index) {
17+
for (let i = 0; i < toDoList.length + 1; i++) {
18+
if (i == index) {
19+
console.log("Mission Removed:", toDoList[i - 1]);
20+
toDoList.splice(i - 1, 1);
21+
}
22+
}
23+
fs.writeFileSync("./todolist.json", JSON.stringify(toDoList));
24+
} else {
25+
let maximum = toDoList.length;
26+
console.log("Please enter a number between the range of 1 and", maximum);
27+
}
28+
} catch {
29+
console.log(
30+
"Hey! Calm down. No need to remove anything. List is already empty!"
31+
);
32+
}
33+
};
34+
35+
module.exports = { utilityRemove };
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
5+
const utilityReset = function() {
6+
let currentData = fs.readFileSync("todolist.json");
7+
let showcase = JSON.parse(currentData);
8+
let resetArray = [];
9+
10+
if (showcase.length === 0) {
11+
console.log("List is already empty");
12+
} else {
13+
console.log("To-do list is reset to zero");
14+
fs.writeFileSync("todolist.json", JSON.stringify(resetArray));
15+
}
16+
};
17+
18+
module.exports = {
19+
utilityReset
20+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
5+
const utilityUpdate = function(index, updateValue) {
6+
try {
7+
let currentData = fs.readFileSync("todolist.json");
8+
9+
let showcase = JSON.parse(currentData);
10+
11+
let toDoList = [];
12+
for (let i = 0; i < showcase.length; i++) {
13+
toDoList.push(showcase[i]);
14+
}
15+
16+
if (updateValue == undefined) {
17+
throw new Error(
18+
console.log("Please enter the update value after item number")
19+
);
20+
}
21+
22+
if (toDoList.length > 0 && toDoList.length + 1 > index) {
23+
for (let i = 0; i < toDoList.length + 1; i++) {
24+
if (i == index) {
25+
console.log(
26+
"Mission Updated:",
27+
"\nOld value was:",
28+
toDoList[i - 1],
29+
"\nNew value is:",
30+
updateValue
31+
);
32+
toDoList.splice(i - 1, 1, { title: updateValue });
33+
}
34+
}
35+
fs.writeFileSync("todolist.json", JSON.stringify(toDoList));
36+
} else {
37+
let maximum = toDoList.length;
38+
console.log("Please enter a number between the range of 1 and", maximum);
39+
}
40+
} catch {
41+
console.log("");
42+
}
43+
};
44+
45+
module.exports = { utilityUpdate };

0 commit comments

Comments
 (0)