I have to celebrate you baby - using object instead of switch/case


#1



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


I am getting a SyntaxError: Unexpected Identifier. Even if i console.log a string before any of the other code, this is the result. I must be missing something fairly fundamental, but I have looked everywhere. The only think I can think of is that codecademy is requiring a switch/case statement because that is what the hint recommends. But is this not an alternative way that should also work? Thank you for any help.


var movies = {
    toyStory: {
        name: 'Toy Story 2',
        review: 'Great story. Mean prospector.'
    }
    findingNemo: {
        name: 'Finding Nemo',
        review: 'Cool animation, and funny turtles.'
    }
    lionKing: {
        name: 'The Lion King',
        review: 'Great songs.'
    }
};

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


#2

You're missing the commas at the end of each value in the movies object.


#3

@dantemp Thank you, that is definitely true. I adjusted the code by adding commas after the ending curly braces of each object inside the movies object. Unfortunately I am still getting the same error. Is there something else I'm missing? Thanks again.


#4

Should be for (var key in movies) {

NOTE: The var keyword here is optional, but, without it, the variable is global.


#5

That was the main error. Once I got the code to do everything I wanted with this solution, codecademy would still present an error message saying "OOPS! Try a switch statement." So apparently they really are looking for a switch/case solution only. Hope this helps. Thanks again @dantemp!