# Fizz

#1

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

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
I believe bothe ways are accurate. They all have conditonal statements. One is different from the other but the second piece of code is working better. Not sure why??

``````

for (x = 0; x<=20; x++){
console.log(x);
if (x % 3){
console.log('Fizz');
} else if (x % 5){
console.log('Buzz');
} else if (x % 3 && 5){
console.log('FizzBuzz');
}
}
or
for (x = 0; x<=20; x++){
console.log(x);
if (x % 3){
console.log('Fizz');
} else if (x % 5){
console.log('Buzz');
}  if (x % 3 && 5){
console.log('FizzBuzz');
}
}

``````

#2

Remember, to test if a number is divisible by some value, you need to make sure that no remainder is returned.

``````x % 3 // -> returns 0
``````

You want that code to be:

``````x % 3 === 0 // -> returns true
``````

Go through your code and fix this for both the 3 and 5.

### Control logic problem

The if/else/if code blocks that you have are causing you a problem. Look at the instructions again:

For numbers divisible by 3, print out âFizzâ.
For numbers divisible by 5, print out âBuzzâ.
For numbers divisible by both 3 and 5, print out âFizzBuzzâ in the console.
Otherwise, just print out the number.

If the number is divisible but BOTH 3 and 5, then print out âFizzBuzzâ.
Else if itâs divisible by 3, print out âFizzâ.
Else if itâs divisible by 5, print out âBuzzâ.
Otherwise, just print out the number.

Look at your code and see if you can refactor it to achieve these requirements.

#3

this is the new code.
for (x = 0; x<=20; x++){
console.log(x);
if (x % 3===0){
console.log(âFizzâ);
} else if (x % 5===0){
console.log(âBuzzâ);
} else if (x % 3 && 5){
console.log(âFizzBuzzâ);
}else{
console.log();
}
}

set them to true. maybe the reason i get the the quiz wrong is the else part. the numbers left over print. donât understand what else to print. Please help.

#4

Youâre getting closer.

Look at the logic.

IF itâs divisible by 3 AND 5, you want to print out FizzBuzz. Okay, but your code wonât print that. Instead, itâll print only âFizzâ. Why?

For example, 15 is evenly divisible by 3 AND 5. It should print âFizzBuzzâ. Why is it only printing out âFizzâ.

Hint: Look at your first conditional expression. You want to reorder your checks to make the first one checking for divisible by 3 AND 5.

#5

can you be a bit more clear.
I have changed it a few times adding true and what not.
for (x = 0; x<=20; x++){
console.log(x);
} if (x % 3 && 5===0){
console.log(âFizzBuzzâ);
} else if (x % 3===0){
console.log(âFizzâ);
} else if (x % 5===0){
console.log(âBuzzâ);
} else{
console.log(x);
}

#6

The order is right. But now stop and look at the first if conditional expression. You have:

``````if (x % 3 && 5 === 0)
``````

Whatâs wrong with each of these conditionals?

• if x is divisible by 3, then 0 remainder
• if x is divisible by 5, the. 0 remainder

Both of these need to be true.

You have this:

``````if (x % 3 && 5 === 0)
# which evaluates to

if (0 && false)

# which evaluates to

if (false)
``````

#7

so this works but donât know what to put in last console.log

for (x = 0; x<=20; x++){
console.log(x);

if (x % 3===0 && x % 5===0){
console.log(âFizzBuzzâ);
} else if (x % 3===0){
console.log(âFizzâ);
} else if (x % 5===0){
console.log(âBuzzâ);
} else{
console.log();
}
}

#8

Look at the instructions again. It tells you what to print out if none of the checks were true.

Otherwise, just print out the number.

#9

could you give me a hint I have tried everything possible even thisâŚ

var FizzBuzz = function(){
for (x = 1; x<=20; x++){
console.log(x);

if (x % 3===0 && x % 5===0){
console.log(âFizzBuzzâ);
} else if (x % 3===0){
console.log(âFizzâ);
} else if (x % 5===0){
console.log(âBuzzâ);

}
}
};
FizzBuzz(x);

#10

Otherwise, just print out the number.

The word âotherwiseâ is the same as `else`. Now, what represents the number in the loop?

``````} else {
console.log( ); // put the symbol (for the number in the loop) into the ( )
}
``````

#11

% and â%â dont run
ran it with function and without
var FizzBuzz = function(){
for (var x = 1; x<=20; x++){
console.log(x);

if (x % 3===0 && x % 5===0){
console.log(âFizzBuzzâ);
} else if (x % 3===0){
console.log(âFizzâ);
} else if (x % 5===0){
console.log(âBuzzâ);

}else{
console.log(â%â);
}
}
};

#13

Nope, `%` is the modulo operator. You use it to get the remainder.

What is the variable in the loop?

Hint: Itâs the one you are using to check if itâs divisible by 3 and/or 5.

``````} else {
console.log( variable goes here );
}
``````

#14

for (var x = 1; x<=20; x++){
console.log(x);

if (x % 3===0 && x % 5===0){
console.log(âFizzBuzzâ);
} else if (x % 3===0){
console.log(âFizzâ);
} else if (x % 5===0){
console.log(âBuzzâ);

}else{
console.log(x);
}
}

#15

WooHoo! You got it! You can get rid of the first `console.log(x)`, which is right after the `for` so that your code looks like this:

``````for (var x = 1; x <= 20; x++) {

if (x % 3 === 0 && x % 5 === 0) {
console.log('FizzBuzz');

} else if (x % 3 === 0) {
console.log('Fizz');

} else if (x % 5 === 0) {
console.log('Buzz');

} else {
console.log(x);
}
}
``````

Does it make sense to you?

#16

Lol. My goodness. That was a headache. Lol.

#17

That was nicely done, hellofromtonya. I do have a question, though. Why do we need to put:
if (i % 3 === 0 && i % 5 === 0) {
console.log(âFizzBuzzâ);
in the first position? That is, why must it be attached to the if statement rather than one of theâŚoh. Never mind. It wonât get to the question of whether itâs evenly divisible by 3 and 5 because it will have already dropped it into the âFizzâ or the âBuzzâ category. Since it is an if/else statement, it doesnât care that it meets the criteria for âFizzBuzzâ as well. Itâs already found a box to put it in and moved on. Ha. Pretty cool.

#18

Exactly. Good job talking through the problem and evaluating each of the checks.

#19

i have a slightly different solution and it worked:
for ( var i=1; i<21; i++) {
if ( i % 3 === 0 && i % 5 === 0) {
console.log(âFizzBuzzâ);
} else if (i % 3 === 0 || i % 5 === 0) {
if (i % 3 === 0) {console.log(âFizzâ);
} else {console.log(âBuzzâ)}
} else { console.log(i) }
}

#20

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