-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.js
71 lines (69 loc) · 1.92 KB
/
function.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
var prompt = require('prompt');
var list = ["apple", "banana", "mango"];
function addItem() {
console.log("current list=>>");
for (var key in list) {
console.log(key + " " + list[key]);
}
prompt.start();
prompt.get(['addItem'], function (err, result) {
var get = false;
for (var key in list) {
if (result.addItem == list[key]) {
console.log("item already exist");
get = true;
}
}
if (get == false) {
list.push(result.addItem);
console.log("view list after adding new items==>");
for (var key in list) {
console.log(key + " " + list[key]);
}
}
});
}
function removeItem() {
console.log("current list=>>");
for (var key in list) {
console.log(list[key]);
}
prompt.start();
prompt.get(['removeItems'], function (err, result) {
var get = false;
for (var key in list) {
if (result.removeItems == list[key]) {
list.splice(key, 1)
get = true;
}
}
if (get == true) {
console.log("view list after removing items==>");
for (var key in list) {
console.log(list[key]);
}
} else {
console.log("item not found to remove");
}
});
}
function searchItem() {
prompt.start();
prompt.get(['searchItem'], function (err, result) {
var get = false;
for (var key in list) {
if (result.searchItem == list[key]) {
console.log("item found", list[key] + " at index" + key);
get = true;
}
}
if (get == false) {
console.log("item not found");
}
});
}
module.exports = {
addItem,
searchItem,
removeItem,
}