"And the good " w/ switch


Hi everybody,
I'm trying to do this exercise with switch rather than if/else, but it returns a "Unexpected token {"
Thank you for your help!


for (i = 1; i <= 20; i ++) {
        switch {
            case (i % 3 === 0):
                console.log( "Fizz");
            case (i % 5 === 0):
            case (i % 3 === 0) && (i % 5 === 0):

SyntaxError: Unexpected token {


I don't think that this will work the way you want at least the way you have set up the switch. in a switch you usually put the expression in the switch and the case would be the expected result for example:

switch(i%3) {
case 0:

also you never set an expression for your switch and the case does't not need to be in parenthesis. Is there any particular reason you wanted to do a switch? the if/else would be much easier and cut down on the code required and reduce the chance of errors in this case. the unexpected token is a result of no switch expression by the way.


Thank you Chris,

I see ! I just wanted to try something else to see if it'd worked, but I guess I need to practice more to get the logic behind this.
It's clear now..

Thanks again !


no problem, I suppose you could make it work but it would just take more logic than it's worth. That's the best way to learn I always try to tweak and add to the examples to try and learn the restrictions and extent of the code. It seems like just when I think I have it down pat i run into some kind of road block or hurdle. I'm just thankful for all the resources we have at our disposal nowadays compared to our predecessors.


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