Need help, again!


#1

this code shoud work but it doesnt, why?

var getReview = function (movie) {

switch (movie) {
    case "Toy Story 2":
        return  ("Great story. Mean prospector.");
    case "Finding Nemo": 
        return  ("Great story. Mean prospector.");
    case "The Lion King":
        return  ("Great songs.");
    default:
        return ("I don't know!");
}

};

getReview(Toy Story 2);


#2

Hi devcoder!

Your code looks good, except you forgot one major part of your switch cases. You need to number your cases, followed with a colon. After each case you need to incorporate "break;". this lets your program know where the end of each case is.

the format is as follows

switch(expression) {

case n:

        code block

        break;

case n:

        code block

        break;

default:

        default code block

}

I may be wrong in areas as I've just picked up coding again.


#3

i do that first but also did work.

var getReview = function (movie) {

switch (movie) {
    case "Toy Story 2":
        return  ("Great story. Mean prospector.");
        break;
    case "Finding Nemo": 
        return  ("Great story. Mean prospector.");
        break;
    case "The Lion King":
        return  ("Great songs.");
        break;
    default:
        return ("I don't know!");
}

};

getReview(Toy Story 2);

i get code error:
SyntaxError: missing ) after argument list


#4

number your cases. in example

switch (movie) {
case 1:
break;

...}


#5

No. That's not the right way to do it. What if you don't know which number represents which movie?

The problem with your code is that you're passing wrong argument in your function. In switch statement you have strings as cases and you are passing Toy Story 2 as an argument that needs to be checked with cases. It's not a string at the moment, therefore you get an error.

Quotes will do the magic.

getReview("Toy Story 2");


#6

there, i do everything that u told me and stil wont work!!

var getReview = function(movie) {

switch (movie) {
    case 1 "Toy Story 2":
        return  ("Great story. Mean prospector.");
        break;
    case 2 "Finding Nemo": 
        return  ("Great story. Mean prospector.");
        break;
    case 3 "The Lion King":
        return  ("Great songs.");
        break;
    default:
        return ("I don't know!");
}

};

getReview("Toy Story 2");


#7

Like this?

var getReview = function(movie) {

switch (movie) {
    case 1 "Toy Story 2":
        return  ("Great story. Mean prospector.");
        break;
    case 2 "Finding Nemo": 
        return  ("Great story. Mean prospector.");
        break;
    case 3 "The Lion King":
        return  ("Great songs.");
        break;
    default:
        return ("I don't know!");
}

};

getReview("Toy Story 2");

But still not work!!!!


#8

What is with that case numbering? Your syntax is bad, thanks to devearl I guess.

The main problem was that you were passing string into getReview function without quotes. Your code should look like this.

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

getReview("Toy Story 2");

By the way, make sure you completely understand how switch statement works. Good luck!