4/33 Is this correct? Why nothing on console?


#1

This is my code:

var movie = prompt("What movie do you want reviewed? Please choose Toy Stoy 2, Finding Nemo or The Lion King.")

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

When I run it, the prompt box opens alright, I enter my movie, and the course turns green and I can continue.

BUT: nothing on console. Is it because we use "return", not "console.log"?

Or have I not linked everything right?

I do struggle with syntax...

Thanks!


#2

The main problem is using "break" and "return" together. Removing break from your code will solve the problem.
But if you want to display the result, you really have to use "console.log" + "return". But this lesson is not about it :grinning:


#3

Ah, ok, thanks for the reply.
This forum is awesome, everyone is super helpful!
One lives and learns.


#4

The main problem is that the function is not called :smile:

You have two options to display a value. First is obviously console.log and the second is to use a console feature that echos the last value unless it is undefined. Now statement like var name = value; do not have a value to be printed in that way and you don't use console.log so why would you expect to see something?

What about calling the function e.g. getReview("The Lion King") or even better console.log(getReview("The Lion King")) at the end of your code?

Also return is not an output (to the screen) it just uses this console feature if it is the very last statement in your code so if you want a reliable output: console.log().

About the break and return. As you already well ... return on return you don't need the break; afterwards as you cannot get there but they are redundant not harmful.


#5

Hope I have understood everything above but my code won't run. I get an error SyntaxError: Unexpected token case

Here is my code:
var getReview = function (movie) {
case "Toy Story 2":
console.log("Great story. Mean prospector") ;
break;
case "Finding Nemo":
console.log("Cool animation, and funny turtles.") ;
break;
case "Finding Nemo":
console.log("Great songs.") ;
break;
};
getReview("Finding Nemo");


#6

The above code has the switch statement missing. Well, I fixed that, and also added the default return string, Also had to change all the console.logs to returns. Codeacademy error message comes up if you use console.log. Now it works OK.


#7

I saw you did not put default in this case. Try to add default case, then run again.


#8

Console.log just writes to the console but maybe there is a test function that compares input vs output e.g. something like this:

if(getReview("Finding Nemo") === "Great songs."){ .... pass}

If you would now use console.log the returned value would not be defined aka undefined but not "Great songs.".


#9

This may help us:
You missed swicth(movie){
You have two "Finding Nemo" and miss "Lion King"
You miss default:


#10

I wanted to post my code and metion that I was missing a comma in one string and a period in another, thus my code would not pass, just an fyi to check your strings for accuracy... lastly this code would also work without the break statements, they are not necessary here

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!");
    break;

}

};

getReview("Toy Story 2");