Why isn't my search function for this object working?


#1

4.4 I have to celebrate you baby

When I first did this exercise I stored all the info in an object. I now know the exercise wants me to use a switch statement, so I'll do that, but I'm confused by why the code below isn't working - it returns the correct result for "Toy Story 2", but returns "I don't know!" for the other two. What's up?


var movieList = {};

movieList.toyStory = {
    name: "Toy Story 2",
    review: "Great story. Mean prospector."
};

movieList.findingNemo = {
    name: "Finding Nemo",
    review: "Cool animation, and funny turtles."
};

movieList.theLionKing = {
    name: "The Lion King",
    review: "Great Songs."
};

var getReview = function(movie) {
    for (var title in movieList) {
        if(movieList[title].name === movie) {
            return movieList[title].review;
        } else {
            return "I don't know!";
        }
    }
};

getReview("Finding Nemo");


#2

a function ends the moment a return keyword is reached, which in your case happens in the first run of the loop

The return i don't know should be after the loop


#3

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