# Fizzbuzz not working :(

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

``````1
``````

2
3
Fizz
4
5
Buzz
6
Fizz
7
8
9
Fizz
10
Buzz
11
12
Fizz
13
14
15
Fizz
16
17
18
Fizz
19
20
Buzz

This is the outcome; as you can see FizzBuzz doesn’t work at 15. Although I think my code looks right, its probably some silly mistake. Thanks for your help!

``` for (var i = 1;i < 21; i++) { console.log(i); if (i % 3 === 0) { console.log("Fizz"); } else if (i % 5 === 0) { console.log("Buzz"); } else if (i % 3 === 0 && i % 5 === 0) { console.log("FizzBuzz"); } }
``<do not remove the three backticks above>``

#2

@hello53,
You will have to use an IF ELSE-IF ELSE-IF ELSE-statement
within a for-loop.

``````program
flow
|
V                                                   + - - -+
for(var i=1;i<21;i++){ - - - - - i >= 21 - - - - - -> | EXIT |
|            |                                        + - - -+
^            V
|         (i < 21)
^            V
|           if (i%3===0 && i%5 ===0 ){
|                console.log("FizzBuzz"); - - - > - - - -+
|           }                                            |
^           else if (i%5===0){                           V
|                console.log("Buzz"); - - - ->- - - - - -+
|           }                                            |
^           else if (i%3===0 ) {                         V
|                console.log("Fizz"); - - - -> - - - - - +
|           }                                            |
|           else {                                       V
|                console.log(i); - - - - - - - - -> - - -+
|           }                                            |
^ - - - - - -< - - - - - - < - - - < - - - - - < - - - - +
}
``````

== the Book ==
javascript remainder operator site:developer.mozilla.org

the for-loop:

== discussions / opinions ==
javascript remainder operator site:stackoverflow.com
http://stackoverflow.com/questions/4467539/javascript-modulo-not-behaving
javascript array site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

== guidance ==
www.crockford.com
http://javascript.crockford.com/survey.html
http://javascript.crockford.com/code.html <<< Javascript coding conventions

#3

That flow diagram is awesome!
Only problem is that it lacks the branching for the conditions (if/else if/else) like shown for the condition of the for loop and that is where @hello53 went wrong.

Think about it @hello53, what happens if i is 15?

#4

@hello53: Notice in your logic, the program will never evaluate FizzBuzz, because it evaluated 15%3 as true before it tries to evaluate 15%3 AND 15%5. Hence @leonhard.wettengmx.n’s correct logic evaluate the FizzBuzz condition first, then do the others.

#5

This program will execute the first down to bottom.
So if you make condition i % 3 === 0 first, this program won’t execute the condition
when it is i % 3 === 0 && i % 5 === 0