diff --git a/src/movies.js b/src/movies.js index 8e2f886d6..163025e1f 100644 --- a/src/movies.js +++ b/src/movies.js @@ -1,17 +1,16 @@ // Iteration 1: All directors? - Get the array of all directors. // _Bonus_: It seems some of the directors had directed multiple movies so they will pop up multiple times in the array of directors. // How could you "clean" a bit this array and make it unified (without duplicates)? -const getAllDirectors = (moviesArray) => { - return [...new Set(moviesArray.map((movie) => movie.director))]; -}; +const getAllDirectors = (moviesArray) => [ + ...new Set(moviesArray.map((movie) => movie.director)), +]; // Iteration 2: Steven Spielberg. The best? - How many drama movies did STEVEN SPIELBERG direct? -const howManyMovies = (moviesArray) => { - return moviesArray.filter( +const howManyMovies = (moviesArray) => + moviesArray.filter( (movie) => movie.director === "Steven Spielberg" && movie.genre.includes("Drama") ).length; -}; // Iteration 3: All scores average - Get the average of all scores with 2 decimals const scoresAverage = (moviesArray) => { @@ -36,37 +35,33 @@ const dramaMoviesScore = (moviesArray) => { }; // Iteration 5: Ordering by year - Order by year, ascending (in growing order) -const orderByYear = (moviesArray) => { - return [...moviesArray].sort((a, b) => +const orderByYear = (moviesArray) => + [...moviesArray].sort((a, b) => a.year === b.year ? a.title.localeCompare(b.title) : a.year - b.year ); -}; // Iteration 6: Alphabetic Order - Order by title and print the first 20 titles -const orderAlphabetically = (moviesArray) => { - return [...moviesArray] +const orderAlphabetically = (moviesArray) => + [...moviesArray] .sort((a, b) => a.title.localeCompare(b.title)) .slice(0, 20) .map((movie) => movie.title); -}; // BONUS - Iteration 7: Time Format - Turn duration of the movies from hours to minutes -const turnHoursToMinutes = (moviesArray) => { - return [...moviesArray].map((movie) => { +const turnHoursToMinutes = (moviesArray) => + [...moviesArray].map((movie) => { const parts = movie.duration.split(" "); let hours = 0, minutes = 0; - if (parts[0]) { - const hoursMatch = parts[0].match(/\d+/); - hours = parseInt(hoursMatch[0]); - } - if (parts[1]) { - const minutesMatch = parts[1].match(/\d+/); - minutes = parseInt(minutesMatch[0]); - } + parts.forEach((part) => { + if (part.includes("h")) { + hours = parseInt(part); + } else if (part.includes("min")) { + minutes = parseInt(part); + } + }); return { ...movie, duration: hours * 60 + minutes }; }); -}; // BONUS - Iteration 8: Best yearly score average - Best yearly score average const bestYearAvg = (moviesArray) => { @@ -90,5 +85,3 @@ const bestYearAvg = (moviesArray) => { ? `The best year was ${bestYear} with an average score of ${bestAvg}` : null; }; - -console.log(bestYearAvg(movies));