Javascript Mastery 1: Arrays (Ex7 Movies)

Hi Guys!

I am going through the JavaScript mastery course right now. I have just completed arrays, and was doing the final exercise. Mosh had a neat solution… I had not even thought that I could have used so many .arrayMethods one after the other in a chain. This is what I was going towards:

//Movies
//array of movies, in which each item is an object is provided
//Get all movies in 2018
//rating must be greater than 4
//pick title and display
note: there is a sort element too, but I am focusing on that.

const movies = [

{ title: “a”, year: 2018, rating: 4.5 },

{ title: “b”, year: 2018, rating: 4.7 },

{ title: “c”, year: 2018, rating: 3 },

{ title: “c”, year: 2017, rating: 5 },

];

function getMovies(array) {

let selectedMovies = [];

for (i = 0; i <= array.length; i++)

if (array[i].year === 2018 && array[i].rating >= 4)

  return selectedMovies.push(array[i].title);

}

console.log(getMovies(movies));

The console just keeps on printing 1. I feel I am pretty close to a solution. But, evidently something is missing! I don’t think the .title or .year or .rating properties are being read (which actually means I am faaaar from a solution).

Any and all help will be appreciated!!!

return leaves your function (and thereby your loop). You’re also returning the return value of .push() which is the length of the array after adding the element. So basically, you are adding the first matching movie to selectedMovies and then return the new length of selectedMovies as the result of you function. And that is of course 1.