Introduction to while loops in js


#1


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


I have no idea what i am doing


var count= 0;

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

loop(1);


#2

There are a few issues, the first and main one being, you are creating an infinite loop. Inside your while loop you are setting count equal to 0 and your while loop is going to run until count is greater than or equal to 3.

Also, because count is already created as a variable you don't need var in front of it. In order to increase count every time it loops, change the count assignment in your while loop to
count ++;

One more minor note, your function is not taking any parameters so you do not need loop(1); you can just have loop();


#3

Maybe this is helpful:
http://www.pythontutor.com/javascript.html#mode=edit

Where to start first?

var count= 0;

This declares and defines a variable that is never used again in your code, but more about this later.

loop(1);

This calls the loop function, but as the function doesn't have a parameter passing it an argument is useless. To explain this:

var my_func = function(parameter){...};
my_func(argument)

where parameter is a local variable inside of the function (and it only exists there), much like if you'd write

var parameter

inside of the function. Where the special thing about it is that it can get a value by using a value in the () of the function call. So for example:

var print = function(msg){
    console.log(msg);
}

print("test"); // --> prints test
print(2); // --> prints 2
print(4); // --> prints4

As you can see this is nice as you can do many things with just one function, all you need to do is to change the input (argument(s)). But obviously for every argument you need a parameter and vice versa otherwise you'll have undefined variables or lost information (like in your case).

And last but not least in your while loop you get to know really interesting and advanced errors :slight_smile:

By using var count = 0; instead of just count = 0 you create another variable that only exists inside of the function and as you now have an ambiguity between the outer count and the inner count javaScript lets you only see the inner count inside and the outer count outside. Even though technically the outer count is global and would be accessible inside the function, this is called shadowing because the outer count is somewhat shadowed by the inner one.

That is why I said that the outer one is never used again although you use a variable that just so happens to have the same name. So to emphasize this, you have 2 variables here that share nothing but the name.

The next funny thing that makes sure that you do not end up in an infinite loop although this seems to be the case (@mkordik might be interesting for you as well) is hoisting. Which means that before anything else is done, the interpreter hoists your local variables to the very beginning of your function. Meaning that count is already known before the loop but without a value. So what happens is that you compare undefined and 3 which fails and so you go on without doing anything.

As said you managed to make pretty advanced errors :wink:

But before you think of just getting rid of the var, which in fact will get rid of the shadowing and hoisting problems, think twice about what your while loop does. As a reminder a while loops runs while the condition is true so you might want to find a condition that is not true all the time.

Hope this helps and is not too confusing. If it is just ask again.


#4

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