...And the good!


#1


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


"Oops, try again. You printed 3 when you should have printed Fizz"
I have this working with for and if loops, but I want to practice with switch/case. How do I make the case "if i%15===0 console.log("FizzBuzz")"? It's skipping all that and going to the default of printing just the increments.


This is what I have:

for (i = 1; i <=20; i++) {
  switch (i) {
    case 'i%15 === 0':
      console.log("FizzBuzz");
      break;
    case 'i%3===0':
      console.log("Fizz");
      break;
    case 'i%5===0':
      console.log("Buzz");
      break;
    default:
      console.log(i);
  }
}


#2

your cases are strings, why?


#3

@stetim94 is right your cases should not be strings.

However that is not the only thing wrong in the code. on your first case you have done

When you need to do

numbers % 3 === 0 && numbers % 5 === 0

As the question is telling you to log "FizzBuzz" when a number is divisible by 3 and 5.


#4

why? give me any number which is divisible by 3 and 5, but not by 15?


#5

Haha exactly what I thought at first, but for some reason it didn't run when i tried that. So makes me think you gotta take the question really literally.


#6

Actually hold up now it works, I'm a mug, must of mistyped or done something dumb


#7

yea, i think you made a typo somewhere, it should work.


#8

even removing the quotes from cases it still prints out 1..20 without replacing the multiples of 3 and 5


#9

I think its because the question is asking you to use nested conditional statements ( if / else if) rather than a switch.

This is how I did it.

for ( var numbers = 1; numbers <= 20; numbers++) {

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

}

However not 100% sure. I'm curious what @stetim94 thinks?


#10

Can we see an updated version of your code?

@oscartbeamish, anything is possible of course, but in this case i wouldn't use a switch statement given you can't do math for cases. Making it really difficult. Your approach is much better


#11

I tried this codes and it worked! Thanks @oscartbeamish


#12

Hi oscarbeamish, can you tell me why you divided first by 15 in the if statement, and then followed the 5 and 3 after? When i did this lesson i did similar to you but i divided first by 3 in the if statement and 5, 15 in the else if. But it didn't work. I don't understand why


#13

the moment one of the condition is true, the remaining else if/else don't run.

Any number divisible by 3 and 5 is also divisible by 3 or 5, so you have to check if a number is divisible by 3 and 5 as first case/if statement.


#14

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