3...And the Good:: Missing after for-loop initializer!

for (var i = 1, i <= 20, i++) {
if (i & 3 == 0 && i & 5 == 0) {
} else if (i & 3 == 0) {
} else if (i & 5 == 0) {
} else {
I’ve checked, double checked, and checked again. I cant find the error in this code.
Keep getting the “Syntax Error: missing after for-loop initializer”.
Any help would be greatly appreciated!

The & is a bitwise AND. Use % remainder operator.

for (var i = 1, i <= 20, i++) {
if (i % 3 == 0 && i % 5 == 0) {
} else if (i % 3 == 0) {
} else if (i % 5 == 0) {
} else {

Silly mistake on my part, regardless, after fixing the syntax error still appears for-loop initializer.

Change commas to semi-colons.

for (var i = 1; i <= 20; i++)

       if((i % 3 === 0 && i % 5 === 0)){
       else if (i % 5 === 0) {
       else if (i % 3 === 0) {
       else {

Thank you for your help @amanuel2, I hope my code looks a little better in this post.
I realized a large rookie mistake in my original code, and what caused my error was not putting semi-colons after my iterations. It stopped the for- loop from starting. After that like you said it was just about moving around the order. Thank you for all your help!

1 Like

So rather than find your mistake, you used copied code? How disappointing. The member didn’t even offer any correction for your mistakes; he just foisted upon you his own linked-to code. Not very helpful, if you ask me.

Who is going to give you code when the rubber meets the road? We have to learn how to solve our syntax and code issues. Actually solving them is the first key.

I told him he got the code correct. He was just missing the order…

I appreciate your concern for my learning @mtf, and although i apologize for your disappointment it was not code i copied that led the me passing this. It was your original point to change my , to ;. The original code i had posted was fine and after i changed the commas. I plan on learning through hard work and repetition. Not by taking the easy way out. I’m not doing this for anyone but myself.

1 Like

Actually there was nothing wrong with the order. His code was all right but for the syntax. As long as the compound conditional is first, and console.log(i) is last, the in between stuff does not matter.

A post was split to a new topic: Incorrect number of items in list - fizzbuzz

Thanks, so the compound conditional has to be first…

I had everything right but that and was wondering why things weren’t working.

I don’t remember the lesson saying that before, even during the lesson of logical operators I didn’t write this down in my notes…

Is this just me, did I miss this tidbit somewhere?


This question just came to me maybe someone can answer this @mtf

*What would happen during an if/else if statement that had multiple compound conditionals?

Can they just be stacked at the beginning of the control flow without error?

Thanks :slight_smile:

The order we write conditional expressions determines which condition or state will divert control flow first.

B O T H ?                                   FIZZBUZZ
<[ NO ]>>   E I T H E R ?                   FIZZ OR BUZZ
            <[ NO ]>>      N E I T H E R    I

Change the order and we change outcome.

1 Like

I didn’t realize this either!

Thanks. This happened to me, I had the right code but in the wrong order.

why does the compound conditional have to be first? can we not have something like this?

var newArray = [1,2,3 …(for sake of example)];
var fizz = “Fizz”;
var buzz = “Buzz”;

for(var i = 0; i < newArray.length; i++){
if(newArray[i]%3 === 0){
else if(newArray[i]%5 === 0){
else if((newArray[i]%3 === 0) && (newArray[i]%5 ===0)){
console.log(fizz + buzz);

The second else if will never be true since 3 and 5 have already taken their own branch. Consequently, when this statement is read, it yields false every time.

done that
my error says: “Unexpected token <=”

If your first line looks like the above, that will explain the error your are getting. The commas make everything a part of the var declaration. Change them to semi-colons ;

 for (var i = 1; i <= 20; i++) {