I could not solve this easy one . Help me?


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

Question : Try it yourself—complete the while loop in the editor so it will print out "I'm learning while loops!". Do this by adding the condition between the parentheses—don't change line 5, or you could get an infinite loop!

Solution :

var understand = true;

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



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

             point of entry
             in while-loop
 ->  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;

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

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.


google search
== the Book ==
javascript while site:developer.mozilla.org

== discussions / opinions ==
javascript while vs do while site:stackoverflow.com

== guidance ==
http://javascript.crockford.com/code.html <== code convention


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