(console.log vs return). Have two different codes and can't understand why one works but the other doesn't


#1

Here is the code which is working fine. I'm using return in this case.

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!";
    }
};

getReview(movie);

Now here is another code which I wrote earlier which doesn't work - and I can't figure out why. Can someone help me with this?

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

console.log("start"); //for debugging
getReview("The Lion King");
console.log("end"); //for debugging

This function returns the following output:

start
Great songs.
end
Great story. Mean prospector.

Why is the last line being printed - or - how is the function being called again AND with a new movie input?


Need review: Switch code results in wrong answer
#2

I think your code is fine...it might be some "control" code within the Codeacademy environment. I have run your code through my node.js installation see below:

This is the code I have used (you code @netpro21265)

⏚ [returntrip:~] 1 % cat script.js
//hardcoding getReview
var getReview = function (movieAsked) {
switch(movieAsked) {
case "Toy Story 2":
console.log("Great story. Mean prospector.");
break;
case "Finding Nemo":
console.log("Cool animation, and funny turtles");
break;
case "The Lion King":
console.log("Great songs.");
break;
default:
console.log("I don't know!");
break;
}
}

console.log("start"); //for debugging
getReview("The Lion King");
console.log("end"); //for debugging

This is the output on the command line:

⏚ [returntrip:~] % node script.js
start
Great songs.
end
⏚ [returntrip:~] %

Please like my post if I have helped you. Thanks!


#3

Thanks for clearing that up.


#4

I think it is because the instructions state to 'return' the review, not console.log the review?