Solution: Older Movies
Let's see the solution to the Older Movies challenge.
We'll cover the following...
Solution
Here, we create the olderMovies()
function that returns the titles of the movies released before 2000.
Let’s have a look at the solution code below:
Press + to interact
const movieList = [{title: "Batman",year: 1989,director: "Tim Burton",imdbRating: 7.6},{title: "Batman Returns",year: 1992,director: "Tim Burton",imdbRating: 7.0},{title: "Batman Forever",year: 1995,director: "Joel Schumacher",imdbRating: 5.4},{title: "Batman & Robin",year: 1997,director: "Joel Schumacher",imdbRating: 3.7},{title: "Batman Begins",year: 2005,director: "Christopher Nolan",imdbRating: 8.3},{title: "The Dark Knight",year: 2008,director: "Christopher Nolan",imdbRating: 9.0},{title: "The Dark Knight Rises",year: 2012,director: "Christopher Nolan",imdbRating: 8.5}];function olderMovies(movieList){const before2000 = movie => movie.year < 2000;const filter = (movies,func) => movies.filter(func);const titles = movie => movie.map(m => m.title);const moviesBefore2000 = titles(filter(movieList,before2000));console.log(moviesBefore2000);}olderMovies(movieList);
Explanation
Line 46-52: Defines a function olderMovies
that returns the titles of all movies released before 2000 in moviesBefore2000
from the given movieList
. Let’s see what’s happening inside this function.
Line 47: Defines a function before2000
that give movies released before 2000.
Line 48: filter
function that takes two parameters i.e movies
and func
that defines the filtering criteria.
Line 49: Creates a function titles
that gives the array of the titles.
Line 50: Calls all three functions and line 51 prints the result.
Access this course and 1400+ top-rated courses and projects.