Not recognising the Switch Statement


#1


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


Hey guys!
Ah- I have no idea what's going on with this code. The code works, returns the right things, however I keep getting the error message saying: "Oops, try again! Better use a switch statement." I think it is pretty obvious that I am indeed using a Switch statement to the human eye but evidently I have done something wrong in my code so that the computer can't recognise it. Furthermore, I have tried the function in many different formats, including having the variable 'check' outside the function, and declaring the function differently (eg; var getReview() = function { ).

I basically have no idea what I've done wrong. Thanks in advance for the help :slight_smile: .


function getReview() {
    var check = prompt("Welcome to Movie Reviews.  Which movie would you like to see a review for?")
    switch (check) {
        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!"
            break;
    };
}
getReview()


#2

delete this line and your code is good:


#3

Thank you for your advice however sadly enough that doesn't work. It now says the variable check is undefined. And isn't it needed either way to input a case?


#4

give the function a param of movies and the switch the same


#5

or couldn't he just cut and paste var check to the outside of the function?


#6

yeah that also works! Oh wow you are so correct!


#7

This is the error I get when putting var check outside of the function. Either in, or out. It's the same error.


#8

Okay- giving just the switch param the value 'movie' gives and error of movie not being defined.

Giving both the switch and function params the value movie gives the 'Way to Go!' but does not work. ie; does not give the value it's supposed to.


#9

can you give us a link, I might be able to look through my code and see what mine looks like compared to yours


#10

Yep. Here's the link again:

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

Thanks.


#11

ok so in mine there is no variable check. and I used the parameter movie for both the switch and the function. try deleting the var check and changing the check parameter to movie


#12

Thank you very much! This is the winning code, with a live getReview() variable which is what I was trying to implement from the beginning.

function getReview(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!"
            break;
    };
}

x=prompt()
getReview(x)

#13

Much appreciated! Thank you for the help.


#14

This is what the SCT is doing.

getReview("Toy Story 2")

which is why you need a parameter. What you call it should not matter unless the SCT is checking that, too.

Bear in mind that anything after return is unreachable so all the break statements can be removed.


#15

no problem. this is actually really cool for me because I am finally good enough at this language that I can help other people out.


#16

would you mind elaborating? I don't really understand how that would work


#17

contradicts,

return signals an exit from the function. Code that follows it is never seen, hence, unreachable. break after return is superfluous code.


#18

okay, so are you saying that we should use console.log instead? I'm not even sure why I used return on this assignment but I did.


#19

No, I think the exercise expects a return value. Just remove the break's.

        case "Toy Story 2":
            return "Great story. Mean prospector."
        case "Finding Nemo":
            // ...

and log the return value to test it.

console.log(getReview("Finding Nemo"));    
// Cool animation, and funny turtles.

There is no need to create a prompt for input as I recall. That's just something folks are tossing in on their own, and probably shouldn't.


#20

ya I just did it and I don't remember there being a prompt, but I didn't know that you could have multiple cases without a break. that's pretty interesting