And the good - syntax error


#1


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

what is the syntax error??


#2

if else? i am pretty sure it is else if


#3

Hi @galbrauner

In JavaScript, conditionals are written with either if, else if or else. Adjust the if else's to else if's.


#4

thank u it worked but now it says Oops, try again. You printed Fizz when you should have printed FizzBuzz


#5

a number which is divisible by 3 and 5 is also divisible by 3 or 5, in other words, if we have 15, which should print fizzbuzz, we first get the if condition:

 if (15 % 3 === 0)

this is true, so fizz is printed

checking if a number is fizzbuzz, should be your first order of operations


#6

got it! thank you! ..


#7

From the standpoint of mathematics, what we are dealing with here is Least Common Multiple or Lowest Common Multiple, referred usually as, LCM.

When testing for divisibility by two or more values, start with testing the LCM first, then the individual values afterward.

This same problem comes up in Functions, only with 3 and 4. Same general concept, though. LCM.


#8

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