11. Review


#1

I wrote this code and it passes, but I'm not sure how.

var count = 0
for (count = 0; count < 1; count++) {
console.log("Hello.");
}

var count = 0
var count = function() {
do {console.log("How are you?");
} while(count = 0)
};

count();

The first part with the for loop I understand. It's this part that confuses me:

var count = 0
var count = function() {
do {console.log("How are you?");
} while(count = 0)
};

count();

This is what printed:

Hello.
How are you?

Since count = 0 and I didn't change this condition inside the body of the loop, shouldn't it loop infinitely? Can someone explain this to me?


#2

@noobydoo,

If you are assigning a zero to the _count variable
the Javascript Interpreter,
will internally convert this zero to a falsy

https://developer.mozilla.org/en-US/docs/Glossary/Falsy
https://developer.mozilla.org/en-US/docs/Glossary/Truthy

To finish up and prove your loop mastery,
write three syntactically correct loops in the editor:
one for,
one while, and one do. Beware of infinite loops!

the for-loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

the while-loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while

the do-while-loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while