Practice makes perfect: What is wrong with my code?


var loop = function(number){
for (var i = number; i < 3; i++){
while(i < 3){
console.log("I'm Looping");


(Never mind I got it. But would still like to know why this wouldn't work.)



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

== discussions / opinions ==
javascript while vs do while

== guidance ==


With your function-call
you will enter into your WHILE-loop

//number being 0 thus i starting at 0
while(0 < 3){
      console.log("I'm Looping");

and you will never leave the WHILE-loop......


You could use the break; statement

while(i < 3){
       console.log("I'm Looping");
       //leaving the WHILE loop without CONDITION check


Or use the return statement
leaving the WHILE loop, the FOR loop and then FUNCTION alltogether...

var loop = function(number){
      for (var i = number; i < 3; i++){
          while(i < 3){
              console.log("I'm Looping");
              return "Leaving the FUNCTION";

console.log( loop(0) );


Thank you very much!


My Code doesn't work. What am I doing wrong here? It doesn't print anything. and says "Are you printing "I'm looping" three times?

Below is my code:

var counter=0;

var loop = function()
//Your code goes here!
console.log("I'm looping!");



Try using a WHILE-condition

counter < 3


I actually surprised myself with doing this from scratch and not looking at Q&A/hint etc. :slightly_smiling:

var count = 0  /*start at zero*/

var loop = function() {   /*function declared*/
	while(count < 3){  /* while count is less than 3 proceed to code */
		console.log("I'm looping!");  /*logs the string */
		count += 1;  /*so until we reach 2, upwards from zero, my count variable is increased by one each time*/

loop(); /*actioning the loop function */


good job my man. just looked at yours. all of the explanations are very informative. i of course couldnt figure it out