Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**The code here prompts the user with 'What would you like to do?'
  2.  * if the string 'new' is entered, the code prompts again with 'Enter a new todo?'
  3.  * the entered string gets pushed to the list
  4.  * After adding a new element to the list, the app prompts again with the first question
  5.  * if the string 'list'is entered, the app shows all the elementsof the list
  6.  * if the string 'quit' is entered the app stops
  7.  * if the string 'delete is returned the app delete a specific item
  8.  */
  9.  
  10. //declare the todos array
  11. var todos = [];
  12.  
  13. //declare the input
  14. var input = prompt("What would you like to do?");
  15.  
  16. //timeout workaround
  17. window.setTimeout(function() {
  18.   //loop keeps prompting for input untill input === "quit"
  19.   while (input !== "quit") {
  20.     if (input === "list") {
  21.       listTodo();
  22.     } else if (input === "new") {
  23.       addTodo();
  24.     } else if (input === "delete") {
  25.       deleteTodo();
  26.     }
  27.   }
  28.   alert("YOU EXITED THE APP!!!");
  29. }, 500);
  30.  
  31. /** lists all element from the todos array */
  32. var listTodo = function() {
  33.   todos.forEach(function(todos, i) {
  34.     alert(i + ": " + todos.toString());
  35.   });
  36.   input = prompt("What would you like to do?");
  37. };
  38.  
  39. /** adds a new element to the todos array */
  40. var addTodo = function() {
  41.   var newtodo = prompt("Enter new todo");
  42.   todos.push(newtodo);
  43.   alert("new task: " + newtodo.toString() + " has been added");
  44.   input = prompt("What would you like to do?");
  45. };
  46.  
  47. /** deletes an element from the todos array */
  48. var deleteTodo = function() {
  49.   var todelete = Number(prompt("What element to delete?"));
  50.   var spliced = todos.splice(todelete, 1);
  51.   alert(spliced.toString() + " has been deleted");
  52.   input = prompt("What would you like to do?");
  53. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement