Introduction to objects 1 4 I dont get where the problem is


#1

So, i did all the stuff the instructions said, and submitted the code. The error comes up saying SyntaxError: Unexpected token case I looked over my code again and I still dont see where the extra case is. Help please :joy:

This is waht i have:

var getReview = function (movie) {
    switch(movie){
        case "Toy Story 2":
            return("Great story. Mean Prospector.");
};
        case "Finding Nemo":
            return("Cool animation and funny turtles.");
};
        case "The Lion King":
            return("Great songs.");
};
        default:
            return("I dont know");
};
};
getReview(movie);

also tell meif i have anything else wrong please :disappointed:


#2

What would } do if there's no matching { ?

Also note that extra semi-colons create empty statements and while that's not going to do anything in most cases, you really shouldn't have empty statements all over the place. Find out whether the statement you're ending ends in semi-colon or not, don't guess (that's like driving a car without holding the steering wheel)


#3

You dont have to put a semicolon after each return,
rather put a break; after each case. And you don't put the closing brackets }; after each case. So the working code will look like this.

var getReview = function (movie) {
switch(movie) {
case "Toy Story 2":
return "Great story. Mean prospector."
break;
case "Finding Nemo":
return "Cool animation, and funny turtles."
break;
case "The Lion King":
return "Great songs."
break;
default:
return "I don't know!"
}
};


#4

return statements do actually end in semi-colons, if you don't put them there then javascript does it for you. I'd claim it's better to put them where it's supposed to be instead of relying on automatic semi-colon insertion, in particular if not having an iron grip understanding of where they are going to end up. It's best to eliminate guesswork and look up documentation for the statement too see whether it ends in semi-colon or not

break's aren't going to execute after return, so they wouldn't do anything!


#5

make sure when you end something with "}", you know where it started, so if there is no starts, there should be no ends --> this probably why it is saying unexpected token. since there are no starting "{", an ending "}" is unexpected.

please make sure for switch, use "break" for each case. otherwise it will follow through till the end.
For example you have 3 cases, then if it matched case 3, then your result will be "result 3" + "default result".
And if it matched case 1, you will get all, e.g.: "result 1" + "result 2" + "result 3" + "default result".

another improvement: for returning a string/text, you wouldnt need () brackets. Yes, it will still work, but you can save some typing and cleaner code.


#6

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