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


#1

for (var i = 1, i <= 20, i++) {
if (i & 3 == 0 && i & 5 == 0) {
console.log("FizzBuzz");
} else if (i & 3 == 0) {
console.log("Fizz");
} else if (i & 5 == 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
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!


#2

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


#3

for (var i = 1, i <= 20, i++) {
if (i % 3 == 0 && i % 5 == 0) {
console.log("FizzBuzz");
} else if (i % 3 == 0) {
console.log("Fizz");
} else if (i % 5 == 0) {
console.log("Buzz");
} else {
console.log(i);
}
}

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


#5

Change commas to semi-colons.


#6

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

    if((i % 3 === 0 && i % 5 === 0)){
    console.log("FizzBuzz");
}
    
    else if (i % 5 === 0) {
    console.log("Buzz");
}
    else if (i % 3 === 0) {
    console.log("Fizz");
}
    else {
    console.log(i);
}

`
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!


#7

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.


#8

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


#9

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.


#10

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.


#11

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


#12

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?

EDIT

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 :slightly_smiling:


#13

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.


#14

I didn't realize this either!


#15

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


#16

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){
console.log(fizz);
}
else if(newArray[i]%5 === 0){
console.log(buzz);
}
else if((newArray[i]%3 === 0) && (newArray[i]%5 ===0)){
console.log(fizz + buzz);
else{
console.log(newArray[i]);
}
};


#17

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.


#18

done that
my error says: "Unexpected token <="


#19

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++) {