4. I have to celebrate your baby


#1

I did everything perfectly..i think.
Where did I go wrong if I read it over and over again.

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

Check the review of Toy Story 2

ar getReview = function (movie) {
switch(movie) {
    case "Toy Story 2":
        // return review here
      return "Great story. Mean prospector." + movie;

    case "Finding Nemo":
        // return review here
        return "Cool animation, and funny turtles." + movie;
        
    case "The Lion King":
        return "Great songs."+ movie;

    default:
        // code to be executed if a movie name is not found
       return "I dont know";
}

};

#2

Well @devninja93146, first of all you haven't added a break statement to any of your case blocks or your default block. Please do that. And also, you just need to return the review, not the review AND the movie, so you can remove your +movie from there.

Do this and reply if the problem continues to persist...


#3

It works thanks Barry !
~~~~~~~
var getReview = function (movie) {
switch(movie) {
case "Toy Story 2":
// return review here
return "Great story. Mean prospector." ;
break;
case "Finding Nemo":
// return review here
return "Cool animation, and funny turtles." ;
break;
case "The Lion King":
return "Great songs.";
break;
default:
// code to be executed if a movie name is not found
return "I dont know";
break;
}

};

~~~~~~


#4

You're very welcome @devninja93146. Keep coding hard :laughing:!


#5

Hey Guyzz i just applied my previous concepts of objects and i just got a lengthy but another way to get the desired output
The Following code I have executed on https://js.do/code/99277 and it is working fine ..kindly have a view on the code. I had left some lines of codes as comments as i was debugging the code, so do not worry about them
enjoy!
// This code is open for optimizations

var getReview = function (movie) {
var getReview = {
toyStory2: {},
findingNemo: {},
theLionKing: {}
}
}
getReview.toyStory2 = {
movieName : "Toy Story 2",
review: "Great story. Mean prospector."
};
getReview.findingNemo = {
movieName : "Finding Nemo",
review: "Cool animation, and funny turtles."
};
getReview.theLionKing = {
movieName : "The Lion King",
review: "Great songs."
};

var counter=0;
//var prop1
var search = function (movie) {
for (var prop in getReview)
{
if(getReview[prop].movieName === movie)
{
//prop1 = prop;
counter++;
console.log(getReview[prop].review);
break;
}

} // for Loop Ends
//console.log(getReview[prop].review);
// console.log(prop);
if(counter==0) {

console.log("I don't Know");
}
} //Function Ends
search("Finding Nemo");

Thank You!


#6

The instructions don't say to use a switch statement, so I went with objects.

var getReview = function (movie) {
    for(var key in MC){
        if(key === movie){
            return(MC.key.review)
        }
    return("I don't know!")
    }
};

var MC = {};
MC.movie0 = {};
MC.movie1 = {};
MC.movie2 = {};
MC.movie0.name = "Toy Story 2";
MC.movie0.review = "Great story. Mean prospector.";
MC.movie1.name = "Finding Nemo";
MC.movie1.review = "Cool animation, and funny turtles.";
MC.movie2.name = "The Lion King";
MC.movie2.review = "Great songs.";

Is there a reason that this is "wrong" and that we "Better use a switch?


#7

Hello @rshinra ,

In this case, you are making a whole new object and setting different values inside it. While this isn't wrong, the switch statement is a much better and faster way of doing this...


#8

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("Toy Story 2");

what's wrong?


#9

Functionally looks good to me and exits '0' as expected.

Only thing missing is the console.log in front of your final function call, like so:

console.log(getReview("Toy Story 2"));


#10

Please help with this code.

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 dont know!";
break;
}
};
var movie=prompt("Enter the Movie name.");
//getReview(movie);


#11

But is it necessary? when you call a function it automatically goes to the function and in this case it goes to function switches the movie name "Toy Story 2" matches the case and returns the review right?


#12

please help me correct my code :

i can't see anything wrong about this code , but error message keeps on popping out :

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

};


#13

Only a small note here to say that the break statements are not mandatory here, as return will exit the function.


#14

Your code is fine. However, it looks like they have made this so specific that even your strings need to be spot on.
When I did this it kept giving me an error because I hadn't added the period (.) at the end of each return.
In your case I noticed that you forgot a space.

return"Great story.Mean prospector.";

Notice that you need a space between the period and Mean.
It wouldn't hurt if you also added a space from your return to string. Two of your returns could use a space. The code should still run but it would look better.


#15

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