10. To learn it, you gotta 'do' it: Browser crash problem!


#1

var yahMon = 1
var getToDaChoppa = function(){
// Write your do/while loop here!
do {
console.log("Yah mon! I was walking down the street");
} while(yahMon < 2){
yahMon++;
}
};

getToDaChoppa();

I just can't get it to not crash my browser!! Why?! Can someone please fix it?


#2

@nintenjoe10,

What they are trying to achieve,
that you are able to use a while-loop.

             point of entry
             in while-loop
                   |
                   V
 ->  while ( condition===true )   -->EXIT--->if condition===false
|       {  // begin of while-code-block  
^
|          // your code that will be executed 
|          //       as long as while-condition === true
|
^<- - - } //end of while-code-block, 
                     while-condition is checked again !!

They give you the code:

var understand = true;

while(understand){
	console.log("I'm learning while loops!");
	//Change the value of 'understand' here!
	understand=false;   //<<== you make sure
                               while-condition===false
}

1 You assign true to the variable understand

2 The while-statement -tests the condition-,
--which is the variable understand with its value being true
3 Access is granted to the while-code-block
4 console.log("I'm learning while loops!"); is executed.
5 assignment of the false value to the variable understand
6 END of while-code-block is reached,
goto 2 //The while-statement -tests the condition-, understand

As understand renders to false,
the while will NOT execute its body,
and will EXIT.

Reference::

google search
== the Book ==
javascript while site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while

== discussions / opinions ==
javascript while vs do while site:stackoverflow.com
http://stackoverflow.com/questions/5599027/the-do-while-statement

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript


#3

Wow, thank you so much!! ...However I actually figured it out already. Sorry xD


#4

Remember you have to do the mycondition===false part to stop the function from doing an infinite loop.


#5

var getToDaChoppa = function(){
// Write your do/while loop here!
mails = 3;
do {
console.log("got a new e-mail");
} while(mails < 4); {
};

getToDaChoppa();
/*what am I doing wrong*/


#6

@blogninja16183_54ebc

You defined the mails variable giving it a number VALUE 3

Then in your DO-WHILE condition you check if mails is less than 4
As you are NOT manipulating the number VALUE of the mails variable
in your DO-WHILE-code-block
the condition will Always be true
and you will NEVER leave the DO-WHILE loop

var getToDaChoppa = function(){
 // Write your do/while loop here!
 mails = 3;
 do {
    console.log("got a new e-mail"); 
    mails = mails + 1;
 } while(mails < 4);

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