Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**The code here prompts the user with 'What would you like to do?'
- * if the string 'new' is entered, the code prompts again with 'Enter a new todo?'
- * the entered string gets pushed to the list
- * After adding a new element to the list, the app prompts again with the first question
- * if the string 'list'is entered, the app shows all the elementsof the list
- * if the string 'quit' is entered the app stops
- * if the string 'delete is returned the app delete a specific item
- */
- //declare the todos array
- var todos = [];
- //declare the input
- var input = prompt("What would you like to do?");
- //timeout workaround
- window.setTimeout(function() {
- //loop keeps prompting for input untill input === "quit"
- while (input !== "quit") {
- if (input === "list") {
- listTodo();
- } else if (input === "new") {
- addTodo();
- } else if (input === "delete") {
- deleteTodo();
- }
- }
- alert("YOU EXITED THE APP!!!");
- }, 500);
- /** lists all element from the todos array */
- var listTodo = function() {
- todos.forEach(function(todos, i) {
- alert(i + ": " + todos.toString());
- });
- input = prompt("What would you like to do?");
- };
- /** adds a new element to the todos array */
- var addTodo = function() {
- var newtodo = prompt("Enter new todo");
- todos.push(newtodo);
- alert("new task: " + newtodo.toString() + " has been added");
- input = prompt("What would you like to do?");
- };
- /** deletes an element from the todos array */
- var deleteTodo = function() {
- var todelete = Number(prompt("What element to delete?"));
- var spliced = todos.splice(todelete, 1);
- alert(spliced.toString() + " has been deleted");
- input = prompt("What would you like to do?");
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement