From f7abd36d3b55bcfcd67c404daceba8bf90d24f99 Mon Sep 17 00:00:00 2001 From: "axartmiami@gmail.com" Date: Tue, 30 Jul 2024 12:31:43 -0400 Subject: [PATCH 1/2] getalldirectors function --- src/movies.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/movies.js b/src/movies.js index 1ad02ff68..4b955e258 100644 --- a/src/movies.js +++ b/src/movies.js @@ -1,7 +1,10 @@ // 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) {} +function getAllDirectors(moviesArray) { + return moviesArray.map(movie => movie.director); +} +console.log(getAllDirectors(movies)); // Iteration 2: Steven Spielberg. The best? - How many drama movies did STEVEN SPIELBERG direct? function howManyMovies(moviesArray) {} From 72c077d3a7019bd7c2ef5be8197eaf10e0bb1e4e Mon Sep 17 00:00:00 2001 From: "axartmiami@gmail.com" Date: Tue, 30 Jul 2024 15:57:53 -0400 Subject: [PATCH 2/2] added the other functions --- src/movies.js | 96 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 87 insertions(+), 9 deletions(-) diff --git a/src/movies.js b/src/movies.js index 4b955e258..4d91efa94 100644 --- a/src/movies.js +++ b/src/movies.js @@ -2,27 +2,105 @@ // _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) { - return moviesArray.map(movie => movie.director); + return moviesArray.map((movie) => movie.director); } -console.log(getAllDirectors(movies)); +const directors = getAllDirectors(moviesArray); // Iteration 2: Steven Spielberg. The best? - How many drama movies did STEVEN SPIELBERG direct? -function howManyMovies(moviesArray) {} +function howManyMovies(moviesArray) { + if (movies.length === 0) return 0; + + const howManyMovies = moviesArray.filter( + (movie) => + movie.director === "Steven Spielberg" && movie.genre.includes("Drama") + ); + return howManyMovies.length; +} // Iteration 3: All scores average - Get the average of all scores with 2 decimals -function scoresAverage(moviesArray) {} +function scoresAverage(moviesArray) { + if (moviesArray.length === 0) return 0; + + const averageScore = moviesArray.reduce((acc, movie) => { + if (movie.score) { + return acc + movie.score; + } else { + return acc; + } + }, 0); + + return Number((averageScore / moviesArray.length).toFixed(2)); +} // Iteration 4: Drama movies - Get the average of Drama Movies -function dramaMoviesScore(moviesArray) {} +function dramaMoviesScore(moviesArray) { + const dramaMovies = moviesArray.filter((movie) => + movie.genre.includes("Drama") + ); + return scoresAverage(dramaMovies); +} // Iteration 5: Ordering by year - Order by year, ascending (in growing order) -function orderByYear(moviesArray) {} +function orderByYear(moviesArray) { + const moviesByYear = moviesArray.slice().sort((a, b) => { + if (a.year === b.year) { + return a.title.localeCompare(b.title); + } else { + return a.year - b.year; + } + }); + return moviesByYear; +} // Iteration 6: Alphabetic Order - Order by title and print the first 20 titles -function orderAlphabetically(moviesArray) {} +function orderAlphabetically(moviesArray) { + const moviesAlphabetically = moviesArray + .map((movie) => movie.title) + .sort((a, b) => a.localeCompare(b)) + .slice(0, 20); + return moviesAlphabetically; +} // BONUS - Iteration 7: Time Format - Turn duration of the movies from hours to minutes -function turnHoursToMinutes(moviesArray) {} +function turnHoursToMinutes(moviesArray) { + const moviesDuration = moviesArray.map((movie) => { + const duration = movie.duration.split(" "); + let totalDuration = 0; + for (let time of duration) { + if (time.includes("h")) { + totalDuration += parseInt(time) * 60; + } else { + totalDuration += parseInt(time); + } + } + return { ...movie, duration: totalDuration }; + }); + return moviesDuration; +} // BONUS - Iteration 8: Best yearly score average - Best yearly score average -function bestYearAvg(moviesArray) {} +function bestYearAvg(moviesArray) { + if (moviesArray.length === 0) return null; + + const moviesByYear = moviesArray.reduce((acc, movie) => { + if (!acc[movie.year]) { + acc[movie.year] = [movie]; + } else { + acc[movie.year].push(movie); + } + return acc; + }, {}); + + let bestYear = 0; + let bestAverage = 0; + + for (let year in moviesByYear) { + const average = scoresAverage(moviesByYear[year]); + if (average > bestAverage) { + bestAverage = average; + bestYear = year; + } + } + + return `The best year was ${bestYear} with an average score of ${bestAverage}`; +}