Console.log(..) outside while loop?

Console.log(..) outside while loop?
0

#1

Im following Colt Steel’s web developer bootcamp and im at Todo List part 1, im wondering what does putting console.log() outside of while loop bellow it does? Does it stop and exit the loop? Since in code we didnt specify condition to be less than something?

Here’s the JS:

var todos = ["Buy New Turtle"];

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


while (input !== "quit") {
    if (input === "list") {
        console.log(todos);
    } else if (input === "new") {
        var newTodo = prompt("Enter new todo");
        todos.push(newTodo);
    }
   
    var input = prompt("What would you like to do?");
}
console.log("OK, YOU QUIT THE APP");


#2

No, the last line only executes once the while loop condition evaluates to true. The while loop will keep iterating until input === “quit”. Once the while loop exits, the console.log at the bottom will display “OK, YOU QUIT THE APP” to the console.

While loops have a condition which is evaluated each iteration. As long as the condition evaluates to true, then it will never stop. In this example, the condition is input !== “quit”, which means as long as input is not equal to “quit”, keep looping.


#3

Wouldnt it be a problem if last line was used very last inside of while loop and last input right before last console.log was used with else alone statement just after two if’s?


#4

Can you write out the code you are describing and what problem you are worried about? I am having trouble understanding what the code you are describing looks like and what you are asking.


#5

Something like this

var todos = ["Buy New Turtle"];

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


while (input !== "quit") {
    if (input === "list") {
        console.log(todos);
    } else if (input === "new") {
        var newTodo = prompt("Enter new todo");
        todos.push(newTodo);
    } else {
        var input = prompt("What would you like to do?");
    }
   
    console.log("OK, YOU QUIT THE APP");
}

#6

The code you wrote above will still exit the while loop when input is equal to “quit”. The only difference is, that “OK, YOU QUIT THE APP” gets displayed to the console each iteration instead of after the while loop is exited. If you enter two todo items, it will display “OK, YOU QUIT THE APP” two times.

So if the question you are asking is “Is it a problem that it displays ‘OK, YOU QUIT THE APP’” inside the while loop, I would say yes, it is a problem if you only want to display that message when you are actually finished adding todo items.


#7

putting var input = Prompt(What would you like to do?) inside of else statement doesnt make typing quit in prompt work. Why?
EDIT: Never mind i figured out its the same as incrementing a number each loop. My bad.