Function not working!

Function not working!
0

#1

Hi all,

I want the function below to return the review for the movie when I input the movie title into my function. Right now it’s not working. Any suggestions?

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

var getReview = function(movie) {
for (var x in movLibrary) {
  if (movLibrary[x]["title"] === movie) {
    return movLibrary[x]["review"];
  }
  else {
    return "Please try again";
  }
}
};

getReview("Finding Nemo");


#2

Your title property is an array, but you are comparing it to a string. Change your object property so it is a string and give it another go …

(I see your review property is an Array as well … Is that a requirement?)


#3

Thanks @michealhall

The revised code works. I’ve pasted below.


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

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

};

getReview("The Lion King");