I have to celebrate you baby - OO version not appreciated by grader


#1


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

I keep getting the error: "Oops, try again. Better use a switch statement", while I verified through the console my code does what it's supposed to do. Namely return the review for the movie, given the title of a movie.

Here's the code that doesn't work:


var movies = [
    {
        title: "Toy Story 2",
        review: "Great story. Mean prospector."
    },
    {
        title: "Finding Nemo",
        review: "Cool animation, and funny turtles."
    },
    {
        title: "The Lion King",
        review: "Great songs."
    }
];

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

In the above I neatly separated the data from the function (though still relying on the existence of the movies variable (which I'd rather use as input to the getReview function as well, but also gives grading problems). This makes it easy to add new movies, while not having to write new logic in the function itself.

However it only passes the grader if I add a fake switch statement (for brevity I left out the movies array of objects below, for its definition, see above):

var getReview = function (movie) {
    switch(movie) {
        default: break;
    }
    for (var index in movies) {
        if (movies[index].title === movie) {
            return movies[index].review;
        }
    }
    return "I don't know!";
};

Please fix the grader codecademy.. so that it checks inputs and outputs, and not whether a specific way of solving the problem is present. Or if you want to check for knowledge of specific techniques: then please mention it explicitly in the exercise...


#2

Did you noticed this?

As per I can say, CA just want to test our previous leant topics. that's the purpose.
There are many codes where CA have asked to use the format it provided? Probable reasons?
1. Best practices
2. Revision of old topics
so there can be many more reasons to add
I hope it helps :slight_smile:


#3

Hi greentreemee,

Thank you for your reply. I did read the part about structures we learned in a previous lesson. However, the lesson I did just before this one was the "Contact List" lesson, where a data structure somewhat similar to the one I use in my code for the movies was being taught (with the only difference being, that I used an array, rather than an object as the container for the movies). Hence, that seemed like the best option to me here.

The hint I didn't really read because it didn't feel like I was really stuck (the solution produced the review of a given movie in the console after all). Also, I did see a few solutions in the forums using switch statements, and that did nudge me in the direction on why the grader was saying I should use a switch statement. So that was helpful.

Personally, though, I kind of disliked that solution, since it has all the movie data hardcoded into the switch statement, and that limits the usefulness of the method. Because each time you have a new movie, you need to add a new case to your switch statement. This way of setting up the method also causes considerable code repetition and thereby violates the Do Not Repeat yourself (DRY) principle: for each new movie you have to write another return statement, while in the code above there are only two types of movies: movies we know about, and those we don't know about. So I don't really see how this case would be in line with best practices.

Thank you for pointing out though, that the purpose here is more testing of existing knowledge. I was maybe a bit too focused on functionality.


#4

I agree , As CA's editor is certainly tested for specific solutions.
Also I do understand what you have pointed out here as I have faced same problems while covering many topics!

:slight_smile:


#5

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