3/33 ..And the good! Possible to use switch instead of if/else if/else?



I was looking at the forums and haven't found anyone using a switch method to complete the exercise. Is this possible to do? When I run the below code, it prints all numbers 1 to 20, but doesn't replace the necessary numbers with Fizz, Buzz, or FizzBuzz. Anyone have any suggestions?

Thank you in advance!

for(i=1; i<=20; i++){
        case i%3 === 0:
        case i%5 === 0:
        case i%3 === 0 && i%5 ===0:


As you've seen, a case expression cannot be an assertion. It is a counterpart of the switch expression.

If we set i as the switch expression, then the cases have to be a value that matches that stored in i. The rather non-standard method is to switch the expressions...

switch (0) {
case i % 15: //...
case i % 3:  //...
case i % 5:  //...
default:     //...

Bear in mind that the exercise does ask us to build an if/else if/else statement, if I'm not mistaken. Good that you took the time out to practice something new, but be sure to follow instructions and pass, first, then play around all you want.


Ahh, I see! Thank you for this! :slight_smile: I did ultimately complete the task using an if/else if/else statement.


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