Lesson 8, Help please


#1


So I am on lesson 8 and have to make the for and while loop, however, whenever i click submit, it ends in an infinite loop and crashes. Can someone please tell me where I'm going wrong?
Thanks


Replace this line with your code. 
var counter = 0
var loop = function() {
    for(var i = counter; i < 10; i ++) {
    while (i < 10) {
        console.log("Still running!");
    }
    }
};
loop();


#2

Hi @userark,

Your while loop causes the crash here because it never becomes false/breaks out, you see when you step through your for loop, you are using the same variable i, which you pass into your while loop, however the while loop will not relinquish control back to the for loop until it can break it's own cycle aka: stop i from being < 10 in this case.
So you start with i = 0 and pass that into the while. We check if 0 < 10, it is and say console.log(), great, then we come back up to the while, we have done nothing with i yet, and so we have i == 0 still, if that < 10? Yep, ok console.log()... and I think you see where this is going :slight_smile:

So you need to find a way to break out of the while loop, either by increment i or by using break, either method would stop you having an infinite loop at this point. But take care what variable you play with because of scope: i exists in both loops. Below is a sample that will hopefully help give a visual on this :slight_smile:

var counter = 0
var loop = function() {
    for(var i = counter; i < 3; i ++) 
    {
        console.log(i);
        j = i;
        while (j < 3) 
        {
            console.log("Still running!" + i);
            j++;
        }
    }
};
loop();

vs

var counter = 0
var loop = function() {
    for(var i = counter; i < 3; i ++) 
    {
        console.log(i);
        while (i < 3) 
        {
            console.log("Still running!" + i);
            i++;
        }
    }
};
loop();

vs

var counter = 0
var loop = function() {
    for(var i = counter; i < 3; i ++) 
    {
        console.log(i);
        while (i < 3) 
        {
            console.log("Still running!" + i);
            break;
        }
    }
};
loop();

#3

Thank you so much. Your help made me realize something then, and that is, does the position of the while loop matter? If I were to place the while loop outside of the for loop, would that mean that it begins with while, and then goes through the for loop, during which it will increase by 1? Or will this merely lead to a second run through the while loop because it will get stuck again on the second go around? Not sure if that makes sense, either way, thanks a lot.
EDIT: So you gave 3 solutions, would each of those work? I think I understand what you meant by scope, and i existing in both loops.
EDIT2: So I switched it to this, however, for some reason it prints out "Still running!" 6 times, rather than just 3 as I thought it should. Any idea as to why?
var counter = 0
var loop = function() {
for(var i = counter; i < 3; i++) {
j = i;
while (j < 3) {
console.log("Still running!");
j++;
}
}
};
loop();

EDIT3:Sorry for so many edits. So I've switched again to this, and get 3 prints, and like it the most as it is most concise, however, I don't understand how it changed, or what the 'break' command does.
var counter = 0
var loop = function() {
for(var i = counter; i < 3; i++) {
while (i < 3) {
console.log("Still running!");
break;
}
}
};
loop();


#4

the position of the while loop certainly will matter, with the two main areas of focus being

  • What conditional check you use in your while loop, as you saw here using the 'i' from the for loop
  • How you intend to escape the while loop, as the 3 examples show, each one displays a different output

You can certainly place the while on the outside, give it a try and see :slight_smile: ) ; the for will then run until it's own condition becomes false or you 'break' out of it. No different to the while in this respect. Just keep in mind if you work with the variable i, then scope really will matter as you will need to declare i outside of while, and declare a different variable inside the for... don't forget find a way to break out of both loops. hopefully the following helps.

var counter = 0
var loop = function() 
{
    var i =0;
    while (i < 3)
    {
        for(var j = i; j < 3; j++) 
        {
            console.log("J is:" + j);
            i++;// this will be different to i outside the for uncomment to test differences
        }
        //i++; //swap comments out to see the difference to the i inside the for loop
    }
};
loop();

With that in mind then, the scope essentially means what parts of the code can see what variables in this case. As you see when using i, how you change i can affect how others see it.
So if i increments in while, then it goes back to the for it stays incremented, and so the for never runs more than once, because i == 10 and that is the conditional check for i.
Same goes with the new example, increment i in the for gives much different results then i outside the for, but still inside the while.

On to the examples/edits :slight_smile:
1) It depends what you need for how they will work for you, you need to judge the requirement of your needs and compare them to the results you get, it is possible neither if these may meet your need but hopefully with your new found understanding you will be able to get there :wink:

2) Rather then give the answer just yet, you seem certainly competent, why not console.log() both the i and j and see if you can work out why, if not post back and someone will try to explain as soon as they can :slight_smile:

3) There are numerous ways to control your loops, you can make a condition false, you can break out or you can continue inside them. Each has it's own merit and use and you would want to spend time researching them to decide but to not get too bogged down we'll look at just the break a minute: A break statement escapes/breaks out of it's loop and continues executing any code after the loop again if you console.log() the results this should help give a visual on what happens, you should notice how the variable i isn't being incremented, or j or whatever letter you choose :wink:


#5

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