Fizz


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<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.