I have to celebrate you baby!


#1

Here's my code. It's working but i don't know if i did it right?
Can someone please explain me how exactly works?

var user = prompt("Which movie would you like to preview?");
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 don't know!";
}

};

Thanks...


#2

Try ths!!!
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 don't know!";

}
};

getReview("Finding Nemo");


#3

Use your prompt Variable(user)....you just store the data in the user var so so pass it in your function
var user = prompt("Which movie would you like to preview?");
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 don't know!";

}
};

getReview(user);


#4

Thank you very much!


#5

Out of curiosity why did you not put the break; at the end of each case in your switch?

Not hole-picking.. just curious if i need to put it in or not, or if i missed something? :slightly_smiling:


#6

i'm not sure, i think it's because we didn't use console.log() in the editor and we don't have to use break. I'm a begginer so i don't know exactly :wink:


#7

If we are returning a value, the switch (and function) is exited without testing any more cases. break is not reachable after return.

Consider the following:

var answer, response;
answer = "yes";
switch(answer) {
case "yes":
    response = "Very well, then.";
    break;
case "no":
    response = "As you wish.";
    break;
default:
    response = "Undecided; okay."
}
console.log(response);

In the above, break serves to jump out of the switch and carry out the logging after the block. The switch and the log out are both in the same scope. We would not use return in this instance. If not inside a function, it would be 'illegal'.

Now consider this switch as being inside a function.

var select = function (answer) {
    switch (answer) {
    case "yes":
        return "Very well, then.";
    case "no":
        return "As you wish.";
    default:
        return "Undecided; okay."
    }
};
console.log(select('yes'));

This time we are not just jumping out of the switch block. We exit the function and return a value to the calling scope to be logged.


#8

Right i think i understand. So break is, as you've said, a way to break out of the switch/code.