"And the good " w/ switch


#1


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!


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


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

SyntaxError: Unexpected token {



#2

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:
console.log("fizz");
break;
default:
console.log(i);
}

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.


#3

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 !


#4

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.


#5

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