4. I have to celebrate you baby


#1

What's wrong with this code? It's supposed to be trying to return the reviews for those movies but I keep getting the following error:

SyntaxError: Illegal return statement

Can't see anything wrong with the return statements, although I'm fairly sure that there's something wrong with the rest of the code.

var movieList = ["Toy Story 2", "Finding Nemo", "The Lion King"]
var getReview = function (movie) {
    for (var i = 0; i < 2; i++) {
        console.log(movieList[i]);
    }
}
    
switch(getReview) {
    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!");
}

#2

You shouldn't use parenthesis in return statements.


#3

Thanks, I've changed it now but it's still giving me the same error:

var movieList = ["Toy Story 2", "Finding Nemo", "The Lion King"]
var getReview = function (movie) {
    for (var i = 0; i < 2; i++) {
        console.log(movieList[i]);
    }
}
    
switch(getReview) {
    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!";
};

#4

Your function should contain the switch statement, so your function can handle the different movies


#5

So like this? It still doesn't seem to work...

var movieList = ["Toy Story 2", "Finding Nemo", "The Lion King"]
var getReview = function (movie) {
    for (var i = 0; i < 2; i++) {
        console.log(movieList[i]);
    

switch(getReview) {
    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!";
}
}
};

#6

There is no need for this. Replace it with a simple function var getReview = function() {


#7

But then how does it know to what it's returning? I now have this but don't see why it would ever return anything:

var getReview = function () {

switch(getReview) {
    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!";
}
};

Strangely codecademy is telling me:

Oops, try again. Better use a switch statement


#8

where did your function parameter go?

If we now call the function:

var getReview = function () {
     
switch(getReview) {
    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("Toy Story 2")

there is no parameter in your function? Add a parameter to your function which then contains the value of the argument we supply at function call

then use the function parameter in the switch statement


#9

Ok that makes sense, thanks for that - however it's still wrong somehow because it's returning I dont' know! instead of the toy story review


#10

What's the latest code you have?


#11

var getReview = function () {
     
switch(getReview) {
    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("Toy Story 2")

#12

What I'd do:

  • Give my function() a parameter
  • Give my switch that same parameter
  • Wrap getReview("Toy Story 2") inside a console.log();

#13

That's amazing thanks, it works! Although the strange thing is that codecademy is stilll giving me the same error:

Oops, try again. Better use a switch statement

but anyway this is my code and it's printing the correct thing, any idea why codecademy is saying it's wrong? just a bug or something else?

var getReview = function (name) {
    switch(name) {
        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!";
    }
};
console.log(getReview("Finding Nemo"));

#14

I got the same error, and I'm really not sure why that is. Could maybe be a bug yes, but considering I'm not the most qualified at Javascript i'd rather not say anything erroneous.

Regardless, you can now move on to the rest of the course :wink:


#15

it's because you changed the parameter in function to 'name', it should be 'movie'. so will look like this:

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!";
    }
};
console.log(getReview("Finding Nemo"));

#16

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.