5. Practice Makes Perfect


#1

This code works, but I can't understand 2 things.
1. function( ) - the parameter is blank but it still works. Why is that?
2. loop ( ); - Same issue, the parameter is blank but the code still works. Why?

//Remember to set your condition outside the loop!
var count = 0;

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

loop();


#2

Because it doesn't take a(ny) parameter(s)

It doesn't take any parameters - you don't need to supply them.


#3

Could we just leave out function( ); and loop( ); and have it still work?


#4

No. function() defines it as a function and loop() calls the function.


#5

i wanna know .. why we didn't use this form .. or why we didn't use ( count = true;) inside the while loop .. like this

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

we have been using this method with the previous examples ..

sorry my english is kindda bad xD

and thank you ^^


#6

Because we can't loop like that?

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

In the while loop above, we loop using numbers. Count starts at a certain number (not defined in this section), then does the code, increments (+1), then if the condition is still true, it will run it again, and keep doing so until it is true. count = true would break the loop because the condition would then be while (true < 3), and that doesn't make sense.


#7

alright i get it .. while loop is checking if count still < 3 .. if true it runs the code .. if not it stops it .. so count = true won't make that happen ..

did i get it or i still missing something ? xD

@jibblyj


#8

You're completely correct.


#10

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