From 7761b981c01f14f6d01617314333fe09f0170d9e Mon Sep 17 00:00:00 2001 From: RickJary Date: Sat, 28 Sep 2024 14:20:52 +0200 Subject: [PATCH] Iterations 1 to 4 done --- src/movies.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/movies.js b/src/movies.js index b665d64f6..f101dc605 100644 --- a/src/movies.js +++ b/src/movies.js @@ -16,14 +16,37 @@ function howManyMovies(moviesArray) { // Iteration 3: All scores average - Get the average of all scores with 2 decimals function scoresAverage(moviesArray) { const sum = moviesArray.reduce(function(acc, movie){ + if (!movie.score) { + movie.score = 0 + } return acc + movie.score; - },0); - return Math.round((sum/moviesArray.length) * 100) / 100; + }, 0); + if(sum === 0){ + return 0; + } + return Math.round((sum/moviesArray.length) * 100) / 100; } -console.log(scoresAverage(movies)) // Iteration 4: Drama movies - Get the average of Drama Movies -function dramaMoviesScore(moviesArray) {} +function dramaMoviesScore(moviesArray) { + const dramaMovie = moviesArray.filter(function(movie){ + return movie.genre.includes("Drama") + + }) + // console.log(dramaMovie) + const sumDramaScores = dramaMovie.reduce(function(acc, movie){ + return acc + movie.score; + }, 0) + // console.log(sumDramaScores) + if(dramaMovie.length === 0){ + return 0; + } + averageDramaScore = sumDramaScores / dramaMovie.length; + return Number(averageDramaScore.toFixed(2)); + +} + +console.log(dramaMoviesScore(movies)) // Iteration 5: Ordering by year - Order by year, ascending (in growing order) function orderByYear(moviesArray) {}