From b897c7f70b8bfde37284a41ce05f644fb789dddf Mon Sep 17 00:00:00 2001 From: benjaminelliott Date: Tue, 25 Jun 2024 20:35:22 +0200 Subject: [PATCH 1/2] Benjamin Elliott --- src/movies.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 77 insertions(+), 8 deletions(-) diff --git a/src/movies.js b/src/movies.js index 1ad02ff68..8e2f886d6 100644 --- a/src/movies.js +++ b/src/movies.js @@ -1,25 +1,94 @@ // 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)? -function getAllDirectors(moviesArray) {} +const getAllDirectors = (moviesArray) => { + return [...new Set(moviesArray.map((movie) => movie.director))]; +}; // Iteration 2: Steven Spielberg. The best? - How many drama movies did STEVEN SPIELBERG direct? -function howManyMovies(moviesArray) {} +const howManyMovies = (moviesArray) => { + return 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 -function scoresAverage(moviesArray) {} +const scoresAverage = (moviesArray) => { + if (moviesArray.length === 0) { + return 0; + } else { + const result = + moviesArray + .filter((movie) => movie.score) + .reduce((acc, movie) => acc + movie.score, 0) / moviesArray.length; + return Number(result.toFixed(2)); + } +}; // Iteration 4: Drama movies - Get the average of Drama Movies -function dramaMoviesScore(moviesArray) {} +const dramaMoviesScore = (moviesArray) => { + const dramaMovies = moviesArray.filter((movie) => + movie.genre.includes("Drama") + ); + const result = dramaMovies.reduce((acc, movie) => acc + movie.score, 0); + return Number((result / dramaMovies.length).toFixed(2)) || 0; +}; // Iteration 5: Ordering by year - Order by year, ascending (in growing order) -function orderByYear(moviesArray) {} +const orderByYear = (moviesArray) => { + return [...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 -function orderAlphabetically(moviesArray) {} +const orderAlphabetically = (moviesArray) => { + return [...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 -function turnHoursToMinutes(moviesArray) {} +const turnHoursToMinutes = (moviesArray) => { + return [...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]); + } + return { ...movie, duration: hours * 60 + minutes }; + }); +}; // BONUS - Iteration 8: Best yearly score average - Best yearly score average -function bestYearAvg(moviesArray) {} +const bestYearAvg = (moviesArray) => { + const movieYears = moviesArray.reduce((acc, movie) => { + if (!acc[movie.year]) { + acc[movie.year] = []; + } + acc[movie.year].push(movie); + return acc; + }, {}); + let bestYear = 0; + let bestAvg = 0; + for (const year in movieYears) { + const avg = scoresAverage(movieYears[year]); + if (avg > bestAvg) { + bestAvg = avg; + bestYear = year; + } + } + return bestYear + ? `The best year was ${bestYear} with an average score of ${bestAvg}` + : null; +}; + +console.log(bestYearAvg(movies)); From 7a48b781140ec99349d11384bf909a19c0dc257b Mon Sep 17 00:00:00 2001 From: benjaminelliott Date: Fri, 27 Sep 2024 17:57:50 +0200 Subject: [PATCH 2/2] done again --- src/movies.js | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) 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));