For line executing perfectly, but while line not doing anything


My FOR loop runs ok. But my WHILE loop doesn't seem to be doing anything. No error messages at all.

var distance = 0;
var i = 0;
var j = 0;

var driving = function(go){
for (i=0; i < 21; i++){
    console.log("You have driven " + distance + " miles.");
    distance = distance+1;
    if (distance < 10) {
        console.log("You aren't there yet.");
    }else if(distance === 11){
        console.log("You're here!");
        console.log("You passed your stop!");

var going = function(){
    while (i < 21){
    console.log("You be driving!");



your while loop, i is already 21? your for loop made i to 21, so the while condition is false the first time its evaluated


AH! Thanks so much. I didn't realize the while loop would get calculated like that after the for loop was completely done.

So is there a way to make the for loop run and then the while loop run in turn each time?


the while loop (which seems like an infinity loop at the moment) runs after the for loop

don't think so, why would you want to do this?


Yeah, I see what you mean about the infinity loop. I managed to fix it this way:

var going = function(){
    while (i < 25){
    console.log("You be driving!");

So it prints out four times at the end (in a kind of useless way).

And I just wanted to know if it was possible to make the FOR and WHILE run together was because the lesson had us make both types of loops on the same page, and they seemed so disconnected. But I guess in a real-world situation I'd just add that action to the "driving" function, right?

Basically I'm just seeing what I can and can't do right now. There's so much to learn. Haha.


nope, the point of the exercise was just so you could recap on both things you learned

yes, you would.

yea, this recap exercise are great so you can experiment on your own what you can and can't do


stetim94, thanks for all your help. It's really encouraging to have such good feedback.


no problem, glad i could help :slight_smile:


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