-1

Doing some practice runs on codecademy and came across the following problem: So the objective is to print "Fizz" if the numbers are divisible by 3. "Buzz" if the numbers are divisible by 5. And "FizzBuzz" if the numbers are divisible by both 3 and 5.

Here is my code, and I thought I had it right, but when I run it they tell me that my code is not 100% accurate. Looking to see any alternatives to this code, or what might be the issue... Code:

for ( i = 0; i < 21; i++)
{
  if (i % 3 == 0 )
  {
    console.log("Fizz");
  }

  if (i % 5 == 0)
  {
    console.log ("Buzz");
  }

  if ( i % 5 == 0 && i % 3 === 0)
  {
    console.log("FizzBuzz");
  }   
  else 
  {
    console.log(i);
  }    
}    
Sebastian
  • 7,729
  • 2
  • 37
  • 69
Edward K.
  • 173
  • 1
  • 2
  • 8
  • For start, Using `else if` would avoid printing both. – Shaunak D May 20 '15 at 06:32
  • Seems you didn't understand the requirements. In the common fizzbuzz problem, the objective is to only print one of these per number. – Bergi May 20 '15 at 06:34
  • 1
    I think you are only supposed to print one result for each number. Your code gives multiple outputs for a number, if multiple conditions match – Markai May 20 '15 at 06:34
  • possible duplicate of [Codecademy FizzBuzz app, stuck on step 1](http://stackoverflow.com/questions/8797834/codecademy-fizzbuzz-app-stuck-on-step-1) – miensol May 20 '15 at 06:35
  • Got it, so the 'else if' prevents all conditions from being printed... – Edward K. May 20 '15 at 06:36
  • You can do it in one line easily (see my answer below): `console.log(i%3?(i%5?i:'buzz'):(i%5?'fizz':'fizzbuzz'));` – connexo May 20 '15 at 06:54

2 Answers2

1

You need to use else if to stop from other conditions executing:

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

It can easily be done in a one liner.

for (i = 1; i <= 20; i++) {
  console.log(i%3?(i%5?i:'buzz'):(i%5?'fizz':'fizzbuzz'));  
};

For a nice formatting also output i on each iteration:

for (i = 1; i < 21; ++i) {
  console.log(i+": "+(i%3?(i%5?i:'buzz'):(i%5?'fizz':'fizzbuzz')));  
};
connexo
  • 53,704
  • 14
  • 91
  • 128
  • Nice and succinct. How did you avoid the == to 0 bit? – Edward K. May 20 '15 at 06:56
  • 1
    I'm using the way Booleans work with **falsy** and **truthy** statements in Javascript. e.g. 9%3 is falsy, since it results to 0. So `if(i%3)` is actually identical in result to `if(i%3!=0)`. – connexo May 20 '15 at 07:00
  • Nice, but you are able to avoid the if else statements with that little "?" you put in the code? – Edward K. May 20 '15 at 07:06
  • 1
    Yes, that the so-called ternary operator. `a > b ? a : b;` is actually identical to `if(a>b) { return a; } else { return b };` – connexo May 20 '15 at 07:09