1

I'm doing an exercise for a class and we are creating a Todo list.

My issue is that when I type in my Todo and press enter, I get the message Enter new Todo, instead of Added todo.

It seems I'm stuck in this else if loop and it won't go to the next else if statement.

var todos = ["Buy New Turtle"];

window.setTimeout(function() {

  var input = prompt("What would you like to do?");

  while(input !== "quit") {

    if(input === "list") {
      console.log("**********");
      todos.forEach(function(todo, i) {
        console.log(i + ": " + todo);
      })
      console.log("**********")
    }
    else if(input === "new") {
      var newTodo = prompt("Enter new todo");
      todos.push(newTodo);
      console.log("Added todo");
      }

    else if(input === "delete"){
      var index = prompt("Enter index of todo to delete");
      todos.splice(index, 1);
      }
  }
    input = prompt("What would you like to do?");

  console.log("OK, YOU QUIT THE APP");
}, 500);

1
  • 1
    You never check the input again (until after the while exits), so the while loops forever Commented Jun 29, 2019 at 23:47

3 Answers 3

2

You need to place the redeclaration of input inside the while loop. Also check if input is truthy - that way if someone closes the prompt box it doesn't crash.

var todos = ["Buy New Turtle"];

window.setTimeout(function() {

  var input = prompt("What would you like to do?");

  while (input !== "quit" && input) {

    if (input === "list") {
      console.log("**********");
      todos.forEach(function(todo, i) {
        console.log(i + ": " + todo);
      })
      console.log("**********")
    } else if (input === "new") {
      var newTodo = prompt("Enter new todo");
      todos.push(newTodo);
      console.log("Added todo");
    } else if (input === "delete") {
      var index = prompt("Enter index of todo to delete");
      todos.splice(index, 1);
    }

    input = prompt("What would you like to do?");
  }

  console.log("OK, YOU QUIT THE APP");
}, 500);

Sign up to request clarification or add additional context in comments.

1 Comment

You beat me to it, this should be the accepted answer
1
var todos = ["Buy New Turtle"];
newTodo = [];

window.setTimeout(function() {

  var input = prompt("What would you like to do?");

  while(input !== "quit") {

    if(input === "list") {
      console.log("**********");
      todos.forEach(function(todo, i) {
        console.log(i + ": " + todo);
      })
      console.log("**********")
    }
    else if(input === "new") {
      var newTodo = prompt("Enter new todo");
      todos.push(newTodo);
      console.log("Added todo");
      }

    else if(input === "delete"){
      var index = prompt("Enter index of todo to delete");
      todos.splice(index, 1);
      }
    input = prompt("What would you like to do?");
  }


  console.log("OK, YOU QUIT THE APP");
}, 500);`enter code here`

Comments

1

Your line:

input = prompt("What would you like to do?"); is outside of your while loop, so everytime you enter a todo, the input variable always has the value of "new".

Look at the attached snippet for corrections:

var todos = ["Buy New Turtle"];

window.setTimeout(function() {

  var input = prompt("What would you like to do?");

  while(input !== "quit") {

    if(input === "list") {
      console.log("**********");
      todos.forEach(function(todo, i) {
        console.log(i + ": " + todo);
      })
      console.log("**********")
    }
    else if(input === "new") {
      var newTodo = prompt("Enter new todo");
      todos.push(newTodo);
      console.log("Added todo");
    }
    else if(input === "delete"){
      var index = prompt("Enter index of todo to delete");
      todos.splice(index, 1);
    }
    // this line was moved into the while loop  
    input = prompt("What would you like to do?");
  }
    
  console.log("OK, YOU QUIT THE APP");
}, 500);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.