Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Greatest movies #3860

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 89 additions & 9 deletions src/movies.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,105 @@
// 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) {
let directors = moviesArray.map((movie)=>{
return movie.director;
});

directors = [...new Set (directors)];
return directors;
}

// Iteration 2: Steven Spielberg. The best? - How many drama movies did STEVEN SPIELBERG direct?
function howManyMovies(moviesArray) {}
function howManyMovies(moviesArray) {
let stevenMovies = moviesArray.filter ((movie) => (movie.director === 'Steven Spielberg') && (movie.genre.includes('Drama')));

return stevenMovies.length;
}

// Iteration 3: All scores average - Get the average of all scores with 2 decimals
function scoresAverage(moviesArray) {}
function scoresAverage(moviesArray) {
if (!moviesArray || moviesArray.length === 0) return 0;

let totalScore = moviesArray.reduce((acc, curr) => acc + (curr.score ? curr.score : 0), 0);

let avg = Number((totalScore / moviesArray.length).toFixed(2));
return avg;
}

// Iteration 4: Drama movies - Get the average of Drama Movies
function dramaMoviesScore(moviesArray) {}
// Iteration 4: Drama movies - Get the average of Drama Movies------------------?????????????????
function dramaMoviesScore(moviesArray) {
let dramaMovies = moviesArray.filter ((movie) => movie.genre.includes('Drama'));
let dramaAvg = scoresAverage(dramaMovies);

return dramaAvg;
}

// Iteration 5: Ordering by year - Order by year, ascending (in growing order)
function orderByYear(moviesArray) {}
function orderByYear(moviesArray) {
let sortedMovies = [...moviesArray];
sortedMovies.sort((a, b) => {
if (a.year !== b.year){
return a.year - b.year;
}
else {
return a.title.localeCompare(b.title);
}
});

return sortedMovies;
}

// Iteration 6: Alphabetic Order - Order by title and print the first 20 titles
function orderAlphabetically(moviesArray) {}
function orderAlphabetically(moviesArray) {
let sortedTitles = moviesArray.map(movie => movie.title);

sortedTitles.sort((a, b) => a.localeCompare(b));

return sortedTitles.slice(0, 20);
}

// BONUS - Iteration 7: Time Format - Turn duration of the movies from hours to minutes
function turnHoursToMinutes(moviesArray) {}
function turnHoursToMinutes(moviesArray) {

let timeFormatted = structuredClone(moviesArray);
timeFormatted.forEach((movieTime) =>{
let durH = Number(movieTime.duration.slice(0, movieTime.duration.indexOf('h')));
let durM = Number(movieTime.duration.slice(movieTime.duration.indexOf(' '), movieTime.duration.indexOf('m')));
movieTime.duration = (durH * 60) + durM;
})
return timeFormatted;
}

// BONUS - Iteration 8: Best yearly score average - Best yearly score average
function bestYearAvg(moviesArray) {}
function bestYearAvg(moviesArray) {
if (!moviesArray || moviesArray.length === 0) return null;

let groupedYear = {};
let bestYear, bestScore = 0;

moviesArray.forEach((movie) =>{
if (groupedYear[movie.year]){
groupedYear[movie.year].totalScore += movie.score;
groupedYear[movie.year].totalMovies ++;
}
else {
groupedYear[movie.year] = {'totalScore' : movie.score,
'totalMovies' : 1};
}
groupedYear[movie.year].avgScore = groupedYear[movie.year].totalScore / groupedYear[movie.year].totalMovies;
});

Object.keys(groupedYear).forEach((year) =>{
if(groupedYear[year].avgScore > bestScore ){
bestScore = groupedYear[year].avgScore;
bestYear = year;
}
else if((groupedYear[year].avgScore === bestScore) && (Number(year) < Number(bestYear))){
bestScore = groupedYear[year].avgScore;
bestYear = year;
}
})

return `The best year was ${bestYear} with an average score of ${bestScore}`;
}