I'm going through Codecademy's Javascript - Intro to Objects course, and am currently hung up on the the "4. I have to celebrate you baby" exercise in the "Review: The story so far..." lesson.
The exercise is:
This exercise has lots of movies and reviews to type in. You might wonder, "Is this teaching coding or typing?!"
But there's a reason why there are so many cases to deal with. We want to show that if we used
if-elsestatements, it would be inefficient. What alternative from the conditionals lesson can we use?Imagine you have a movie collection and you want to write some code that assigns your review to each of them. Obviously each review differs depending on the movie. Below are the movies and your review. Use a structure learned in an earlier lesson to write code for the information below:
- "Matrix" - "good trip out"
- "Princess Bride" - "awesome date night movie"
- "Welcome to America" - "Amjad's favorite"
- "Remember the Titans" - "love the sports"
- "Why do I look like I'm 12?" - "The Ryan and Zach story"
- "Fighting Kangaroos in the wild" - "Token Australian movie for Leng"
getReviewshould be a function that takes a movie name andreturns its review based on the information above. If given a movie name not found just return"I don't know!".
My understanding is that it is important to separate data from logic. So, my initial solution was:
var getReview = function (movie) {
for (var i = 0; i < movieReviews.length; i++) {
if (movie === movieReviews[i].name) {
return(movieReviews[i].review);
}
}
return("I don't know!");
};
var movieReviews = [{name: "Matrix", review:"good trip out"},
{name: "Princess Bride", review:"awesome date night movie"},
{name: "Welcome to America", review:"Amjad's favorite"},
{name: "Remember the Titans", review:"love the sports"},
{name: "Why do I look like I'm 12?", review:"The Ryan and Zach story"},
{name: "Fighting Kangaroos in the wild", review:"Token Australian movie for Leng"}];
console.log(getReview("Matrix"));
I'm sure there are ways to optimize this, but overall I think it would be easier to add, edit, modify, etc. the movieReviews array than it would be to program switch statements.
In otherwords, I'm not seeing why a switch statement would be inefficient in comparison to an if-else statement. What am I missing?
EDIT: The help text for the problem is:
It is possible to use if, else if and else statements, but that is inefficient. In situations where you have lots of different scenarios and different comes, try using a switch statement!
Because we are defining a function, we can make use of the return keyword!
Make sure that what you return matches the case of the review text
switchgetReviewis a good first step towards it. Instead of writing something like:if (movie == "Matrix") // Do something else if (movie == "Princess Bride") // Do something else // ....You are looping through an Array to find the answer. Way less code than the poorif/elseif approach I wrote above. Now, is there a better way to find the names rather than an Array?Oops, try again. Better use a switch statement. When I view the exercise 'help text' it tells me that if/else statements are inefficient and that I should be using a switch statement. (Added the 'help text' to my main question.) But I don't see why a switch would be more optimal than an if/else in this case.iftoswitch