3/33 and the good


#1

Here is code:
for (var i = 1; i < 21; 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");
}
else {
console.log(i);
}
}
Prints this:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz
16
17
Fizz
19
Buzz

Help?


#2

@trapnation,
You will have to put the most-specific condition ((i % 3 === 0) && (i % 5 === 0 ))
as first-condition


#4

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);
}
use this code inside the loop


#5

@mangalbeta,
Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= http://discuss.codecademy.com/t/using-backticks-to-format-your-code/3697


#6

why do we have to put the condition ((i % 3 === 0) && (i % 5 === 0 ))
as first-condition ?


#7

This is the code that worked for me

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)
}
};