3. I'm having trouble with my code. Could someone please help


#1

This is my code.

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

And I keep getting "SyntaxError: Expected ; "


#2

Look at the first condition of your for loop. You are using === while this may work in Javascript, Java only has support for ==.


#3

I'm using JavaScript


#4

Hi @soon2bcoder

I tried to do your code, it works.

for(var i = 1; i < 21; 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 hope it's that what you want.

Hold on :wink:


#5

whoops sorry, I thought I was still on the Java forum. My mistake.


#6

That's okay. :smile:


#7

When I tried that it worked. Thanks. Also do you know why my code wouldn't run?


#8

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

the for loop consists of three major components.
(initialization, condition, postfix/ incrementation)

right now you're using a comparison operator " === " instead of an initialization operator " = " in this line.

Secondly!

if(i % 3 || 5){
    if(i % 3 && 5){

are not complete comarisons. The if statement doesn't know what you're talking about right now. Because the modulo is finding the remainder of a number try changing it to this.

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

same with your third if statement.

Lastely, I'm fairly certain you will never be able to print out "FizzBuzz" because you cannot have "either, and". Try re-writing your if statements to utilize the "else if" properties like so"

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

This should hopefully keep further errors from forming in your code on this lesson.


#9

The answer of @lolman is really complete, you should look it :smile:


#10

Thank you! :smile: This really helps.


#11

Thanks for helping out! :grin: I spent like an hour figuring out what I did wrong, but I couldn't find it. Seems I had to write this
(i % 3 === 0 && i % 5 === 0) in the if code, which I didn't. I put it up at last in an else if code, which didn't make it work.