-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.js
47 lines (41 loc) · 1.11 KB
/
list.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
// collect the list of items in array
var todos = ["Buy a hammock"];
//prompt user
var input = prompt("What would you like to do?");
while (input !== "quit") {
if (input === "list") {
listTodos();
} else if (input === "new") {
addTodo();
} else if (input === "delete") {
deleteTodo();
} else if (input === "quit") {
break;
}
//ask again for input
input = prompt("What would you like to do?");
}
console.log("Ok you quit the app");
function listTodos() {
console.log("***************");
todos.forEach(function(todo, index) {
console.log(index + ": " + todo);
});
console.log("***************");
}
function addTodo() {
//as for new todo
var newTodo = prompt("Enter new todo");
//add to array
todos.push(newTodo);
console.log("Added todo.")
}
function deleteTodo() {
var input = prompt("Enter which todo to delete");
var index = parseInt(input);
if (Number.isInteger(index)) {
var removed = todos.splice(index, 1);
console.log(todos);
console.log("We just removed: " + removed[0]);
}
}