10. To learn it


#1

var getToDaChoppa = function(){
// Write your do/while loop here!
var a = false;
do {
console.log("getToDaChoppa")}
while (a = true) ;

};

getToDaChoppa();

Just curious why does it crash my browser? i triied "while (a===true") and also
" var a = false;
do {
console.log("getToDaChoppa")}
while (a =false)
a = true ; "
and it worked.

so why didint it work the first time?


#2

because it is a infinity loop. a === true should work, because then you are comparing a with true, which is not the case, so the condition is false, the loop ends.

On the other hand, if you use just a single equal sign, you assign true to a. Which means a is now true, which means the condition evaluates to true, and will run forever.

Same for false in your last example. a is set to false, so the loop evaluates to false, the loop ends. It is very risky to use the assign operator (single equal sign) in this sort of situations, most of the times we use the compare operator (2 or 3 equal signs)


#3

After reading about the infinity loops and also crashing my browser, I was hung up on this exercise for 1hour. My end result

var getToDaChoppa = function(){
var b = false
do {
    console.log("Otie is the best.");
} while(b);
}
getToDaChoppa();

I was inspired by the format used by another CodeAcademy student here.
I'm not satifsfied because I do not understand why this code does not make an infinite loop. Shouldn't it crash since I'm assigning b to false and also not providing the b = true to prevent the browser from crashing?


#4

a loops runs as long as the condition is true:

do {
   // do something
}while(true)

dont run this, it is infinity loop. Does that make sense? Okay, well. b is false, the loops ends the moment the condition is false (which is the case). What the exercise wants you to do, is compare b (2 or 3 equal signs) with true:

var getToDaChoppa = function(){
var b = false
do {
    console.log("Otie is the best.");
} while(b == true);
}
getToDaChoppa();

this code you can run, it won't crash. Why not? Because b is false. and false == true is false, so the loop ends, however doing this:

var getToDaChoppa = function(){
var b = false
do {
    console.log("Otie is the best.");
} while(b = true);
}
getToDaChoppa();

is really dangerous (again, don't run this code) it is a infinity loop. Now you assign true to your variable b, meaning that the condition will always be true, and run forever


#5

I have stuck on this too. This is how I understood it and it works. Do..while is different from while loop. It doesn't execute code in do and then proceed to while loop. It's flow looks like:
1. Execute code in do block
2. Check condition in while
2.1. If condition is true, go to point 1.
2.2. If not, end loop.