Practice makes perfect, #5, It works but no idea why!


#1

I'm stuck on this lesson:

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

This is my code which was a complete guess after some trial and error. It does work but I actually don't understand why, or what is going on! I'm particularly confused by "loop( );" what is it doing? I would be so grateful if someone could break it down for me.


var count = 0
var loop = function(){
	while(count < 3){
		console.log("I'm looping!");
		count++
	}
};

loop();


#2

while loop:
The while loop loops through a block of code as long as a specified condition is true.
here your condition is while(count < 3) it means as long as count is less than 3 ,the code in the loop will run.
NOTE: If you forget to increase the variable used in the condition, the loop will never end. This will crash your browser.

HOW IT WORKS:
count = 0;
while(count < 3) so now while(0 < 3) == true
then console I'm looping!
count++ ; now count=1
then again check while(1 < 3) == true
then console I'm looping!
count++ ; now count=2
then again check while(2 < 3) == true
then console I'm looping!
count++ ; now count=3
then again check while(3 < 3) == false

************** loop end ******************


#3

Thanks,
That makes more sense now, however I still don't understand what loop ( ); is doing?
If it is making the function run again why is it placed at the end of the code block and not at the beginning?


#4

its not making the function run again and loop(); what is making the function run.you can use it befor/after function.when you call the function( loop(); //calling the function ) it runs.without calling it'll run.

Run this without function

count=0;
while(count < 3){
console.log("I'm looping!");
count++
}


#5

Still not understanding this:confused:

I run the code block without the function and the console logs:
I'm looping!
I'm looping!
I'm looping!
2

I have a 2 appear which I don't want.

I still don't understand what the bits in bold are doing.

var count = 0
var loop = function(){-- what is this doing?
while(count < 3){
console.log("I'm looping!");
count++
}
};
loop( ); -- what is this doing?


#6

var loop = function(){-- what is this doing?
you took a variable named loop and a function in it.loop is just a name of a varible nothing else.you can take any name here.but remember you have to call by same name.

next

loop( ); -- what is this doing?
now here you called the function.without calling function doesn't run.

Do you know what is function.how does it work?


#7

Ok this is starting to make more sense. But I realise I need to go over functions again!
Thanks for your help.


#8

@j-nizzle: I was wondering the same thing about why I needed the loop(); Eventually I realized it was just there at the end to activate the loop function. That's all. :slight_smile:

I've gone over JS functions many times and still sometimes I forget details of how the JS functions work as soon as I use them in a new context. I guess I just wanted to let you know, that was great question and thank you for asking it cause it reminded me of what I was missing . :slight_smile:


#9

var loop = function() {
This tells your computer that you are creating a variable (var), in this case naming it loop, and that it is equal to a function (function() {};). This tells the browser to do whatever is inside the curly braces when the function is called.
loop();
This line is "calling" the function. Whatever you put inside the curly braces will only run if you "call" your function name, in this case "loop();"
Hope this helped!!!:grin:


#10

var count = 0

This code simply tells your computer that the variable "count" is equal to zero (which will be important later on in your program.

var loop = function() {

This code states that the variable "loop" will be equal to "function() {};". This tells the computer that once the code is "called" or activated, it should run the code within it. Function() {} is simply a command that tells the computer to run the code inside its curly braces.

while(count < 3) {

This code starts the while loop. It tells the computer to do the code within its curly braces until some condition is met. In this case, the condition is "count < 3". The computer asks itself whether the variable "count" is less than 3, which in this case it is (0 < 3 === true), so it runs the code within it. The next time (1 < 3 === true), the code will also be run, and also the time after that (2 < 3 === true), but each time after that (3 < 3 === false) the code will be skipped over because the condition will have been met. This will be explained more thoroughly later.

console.log("I'm looping");

Since this code is inside the while loop, it is only being run if it has been "called" and if the while loop's has not yet been met. If this is true, the computer will log whatever has been placed in the parentheses "I'm looping." If the function either has not been called or the condition has been met, the computer will overlook this code.

count++

This is simply an easier way of typing count + 1 (because you know how we programmers get lazy :joy:). This code tells the computer to change the variable count to 1 more than it was before. (This is how I got those numbers when explaining the step with the condition.) The computer keeps repeating the block of code and changing the variable, until the condition is false, in this case if "count" isn't less than 3.

loop();

Lastly, this small block of code simply "calls" the function. This just means that it tells the computer to activate it.
I really hope this helped. It's great that you care what the code means, not just that it works. :grin::grinning::smile:


#11

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