8/11 JS - When to 'while' and when to 'for'


Few questions:
1. Why doesn't my display print out "for loop"?
2. Why does my window display "false":
a) Is it because I assign it to answer or
b) the condition is false as it tries to go through the 'while loop' again?

Thanks in advance!!


for (var count = 1; count>2; count++) {
console.log("for loop");
count = count+1;

var answer=true;
while (answer) {
console.log("while loop");
answer = false;

while loop


your for loop is false

you don't need count = count+1;
because for loop have count++ instruction
your condition for for loop is "if count is greater than 2 then i go in the code of for loop"
you need "if count is smaller than 2 then i go in the code of for loop"

try to use

for(var count = 1; count < 2; count++) {
    console.log("for loop");

sorry for my bad english


Ok 1. is already covered by @aureolebigben and 2. is what you assumed as a). Many webconsoles have the feature that they give a quick response in displaying the last value to the user. You can test it just write a one-liner and see the output (unless it is undefined like the value of console.log). So in your code that last value is this assignment.