Practice makes perfect: What is wrong with my code?


#1

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

loop(0);

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


#2

@cwaysports,

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

@cwaysports,
With your function-call
loop(0);
you will enter into your WHILE-loop
with

//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
       break;
}

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

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) );

#4

@leonhard.wettengmx.n
Thank you very much!


#5

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()
{
while(counter===3)
{
//Your code goes here!
console.log("I'm looping!");
counter++;
}
}

loop();


#6

@aliee,
Try using a WHILE-condition
like

counter < 3

#7

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 */

#8

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