Lesson 10 Explanation


#1

I got my solution to lesson 10 to work, but I don't quite understand why it works, which is obviously the whole point of learning. Here is the code:

var billy = false
var getToDaChoppa = function()
{
do
{
console.log("Billyyyyyyyy! Get to da choppaaaaa!"); }
while(billy);
}
getToDaChoppa();

So I create a variable called "billy" which is set to false. In the loop I am telling it to "do" log the string of text while "billy". But what exactly does that mean? Do it while billy is false? If billy is set to false initially, why does the loop stop. Is something changing the value of billy that I'm missing?


#2

You are missing ; after you set value to var billy = false. It should be var billy = false;.
Now, for the explanation.
You say your do while loop to do something once and then check if given condition is true. That means that do while will always run at least once.
Because you set your condition to variable that is set to false, your code will run once, and that's it.

You question was also this

If billy is set to false initially, why does the loop stop

If you set loop condition to check while (billy === false), your loop would continue, and you would have infinite loop. But with condition as it is, while (billy), it will stop after first looping because it will evaluate to while (false), which will lead to the loop's end.


#3

@ajaxblaster34969,

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