FIzzBuzz exercise 2


#1


so this code returned stuff but I still got an error too!! any ideas what that error is referring to.


https://www.codecademy.com/en/courses/spencer-sandbox/0/3?curriculum_id=506324b3a7dffd00020bf661#


Oops, try again. It looks like you printed out the wrong number of items.


for(var i = 0; i <= 20; i ++) {

if ((i % 3 === 0) || (i % 5 === 0)) {
    console.log("Fizz") || ("Buzz");
    
}
else {
    console.log("FizzBuzz");
    
}
};

I also tried this version:
for(var i = 0; i < 21; i ++) {


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


#2

This is the correct output:

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

You have to implement this part of instructions:

Otherwise, just print out the number.

By otherwise it means that if number is not divisible by 3 and is not divisible by 5.


#3

My input is as follows:
Fizz
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz
16
17
Fizz
19
Buzz

I don't see any FizzBuzz!! not sure why that didn't return any value?

This is my code:
for(var i = 0; 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);

}
};

any suggestions?


#4

Ok, let's say that we are inside the for loop and i = 15.

Condition of the if is i % 3 === 0 -> 15 % 3 === 0 -> 0 === 0 -> true. So interpreter will print out "Fizz" and will leave the if ... else if ... else construction.

You have to rearrange your code. You should check if a number is divisible by 3 and by 5 in the first place.


#6

you want to put the if its divisible by 3 and 5 to the top of the other IFs. Otherwise, the 2 IFs that console fizz and buzz must give more specific argument such as:
if(i % 3 === 0 && i % 5 !==0) {
console.log("Fizz")
}
if "i" is divisible by 5 and "i" is not divisible by three, console log Buzz


#7

don't forget to start with i=1, if that's your problem


#9

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