2.While Syntax


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-ASGIv/0/2?curriculum_id=506324b3a7dffd00020bf661

I am soo confused as to what the ■■■■ i have to do in this lesson. In the while loop condition i enter understand = 1 but when i run it it just screws up my browser

var understand = true;

while( understand = 1) {
    console.log("I'm learning while loops!");
    understand = false;
}


#2

@triple-a,
If you want to create a condition
you will have to use the comparison operator which in Javascript syntax is expressed by either == or ===
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

====================================================

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 <== code convention
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript


#3

Thanks- I was stuck on the same exercises. Thanks to your post I figured it out.
It works if you use understand===true in the parenthesis.


#4

It took me awhile to realize that the variable 'understand' is initially set as 'true', so when entering 'understand ==== true' in the brackets, the while loop will run. During the running of the loop the variable 'understand' is then changed to false, so when the loop is run again, it now is false, and ends.

May be obvious to some, but helpful to others. Took me a second to figure out.


#5

@makautomatik
and as the Javascript interpreter expects a Boolean between =while='s parenthesis-( )
you could also have used

while( understand ) {
....
}

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.