For and While loops not working


I'm supposed to be doing both a for loop and a while loop and it's been a few weeks since I have done either. I've looked back at previous exercises and still can't figure it out.

I'm getting the error: "SyntaxError: missing after for-loop initializer" and I'm not sure what that means exactly.

for (var count = 0, count < 12, count++) {
    while (count < 10) {


Hi this line

fot (var count = 0, count < 12, count++)

change all the , with ;


Hi @jaysem,

Although wizmarco is completely right, well minus the typo on his fot :wink: heehee, I would advise against running the code you have as this will cause an infinite loop. :slight_smile:

Having a quick look over the lesson, although you could try for something as complex as you wrote, you can actually pass just by writing two separate loops as the following code sample below shows.

var whileVar = 3; //declare a variable

while(whileVar > 0) //set the condition to check for the while loop: This is a true condition

{// What should happen, each cycle of the while loop, as long as the condition set above is true
    console.log("This is the value of the whileVar: "+whileVar);
    whileVar --;//Help close the loop by drawing towards an end
//end while loop

//A for loop following the basics of the lesson
//Start Condition, End Condition ; Increment Operator
for(forVar = 5; forVar < 50; forVar = forVar + 10)
   console.log("This is the value of the forVar: "+forVar);
//end for loop


Thank you @mike_in_training. I will definitely go for something easier like you just described. Can you explain to me why my previous loop with the punctuation correction would be an infinite loop? I'm not understanding why it would be and I would like to learn that way I don't make that kind of mistake in the future.


Hopefully this will be clear, there is a reason I have "in training" in my name lol :wink:

So your above loops @jaysem, work appx. as follows.

     /*some 'count' == 0*/  /*for as long as it is true (step in to the code block).....*/     /*afterwards do this*/
for (var count = 0             ;                       count < 12                                          ;  count ++)
{ /*here is the action I want you to process each pass through, as long as the condition is true*/
    while ('count' < 10) //this is a conditional check, this is part of the problem...
    {  /*go ahead and print something in the console for me*/
           console.log(count); //this is all I have asked the while loop to do, the other part of the problem 
    } //now end my while loop once count >= 10, as my conditional check for the while loop states above
} //here ends my for loop, once the conditional check I set is met; which is that count would be >= 12

So to break this down, and without adding too much in this post, I hope :innocent:
The issue occurs here because of the variable count you are using in the while loop, the condition the while loop is checking on and finally how the variable count is in play.
You declare and set the variable count as 0 in the for loop: So count == 0 at this point.
Your for loop, then hits the conditional check and asks, is count < 12? 0 == < 12 this is true so I will perform what I am asked. This is all good at this moment.
Now then we step into the for loops code block which starts by the while stating, as long as count is < 10 do something. So count currently stepped in as 0, 0 == < 10 so this is true, which means I must perform my action as long as this is true. (Here's the key; as long as this is true I must carry on performing my action)
so along comes the while loop, it prints console.log a few times, and the while loop is selfish here, we didn't ask it to do anything else at all, just print. So that's what it will do.
The while loop will print something to the console. Then it performs no other action, so it cycles back up and asks, hey how much is count worth? Oh it == 0, is that < 10, why yes it is, ok I'll carry on... and it will repeat that cycle endlessly, hence the infinite loop :slight_smile:

So hopefully, you can see there is just one tiny missing key here, what if we increase the value of count each cycle of the loop so that it can eventually become false and therefore step out of the loop?
Why don't we have the while loop do this for us by simply setting count to +x each cycle?

and hopefully then this makes sense :slight_smile:

for (var count = 0; count < 12; count++) {
    console.log("my for count = " + count);
    while (count < 10) {
        console.log("    my while count = " +count);
        count++; //increase count in here, this helps us to reach the end goal that the while can finally be false

Admittedly using an independent variable in the while to the for loop would be just as safe, something like the following example. The idea of all the console.log() statements in both examples is to try and how how the flow of these two loops work.
Notice how after the first for loop print, the while loop must be completed before it hands back off to the for loop?

var myWhileVar = 5;
for (var count = 0; count < 12; count++) {
    console.log("my for count = " + count);
    while (myWhileVar < 50) { //now we don't care about count
        console.log("    my myWhileVar = " +myWhileVar);
        myWhileVar += 5; //but we still need to try and make while false eventually

Finally, you can also write something more like this if you would like to iterate between the two.

var myWhileVar = 2;
for (var count = 0; count < 6; count++) {
    console.log("my for count = " + count);
    while (myWhileVar < 10) {
        console.log("    my myWhileVar = " +myWhileVar);
        myWhileVar += 2;
    myWhileVar = 2; //this will reset the while so it iterates once for each for loop cycle

I apologise in advance I'm not very good at this, but please the fact you want to learn and you have expressed an interest I strongly encourage you to reply if this is unclear or you need more understanding, although it is true you should always seek other resources whilst learning to broaden your scope, and I recommend you do check other sites out etc. :slight_smile: this community has some great strong members who can act as those resources if you would like to interact rather than just read a webpage :slight_smile:


@mike_in_training Well you may be in training but you are far more advanced that I am. I understood most of what you said. Correct me if I misunderstood but the issue is that the while loop will keep going until it is finished instead of checking back with the for loop after each go around. So the while loop acts independently until it's cycle stops?


You got it with one tiny point, the while loop as was originally written could never finish because it wasn't asked to do anything that could set the condition it checks on, to false, and essentially that's how while loops work.


If we therefore never give it a way to NOT be true (false), then it is just doing what it was told :slight_smile:
Hopefully if you can take advantage of posting some of the snippets to see the output this will help :slight_smile:

Really should pat yourself on the back here @jaysem, you've persisted greatly and you're showing a fine understanding :slight_smile: NICE JOB!


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