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

Done w/ Bonus #3862

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2001,3 +2001,5 @@ const movies = [
score: 8
}
];

module.exports = movies;
65 changes: 57 additions & 8 deletions src/movies.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
// const movies = require('./data.js');

// 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 [...new Set(moviesArray.map((element) => element.director))]
}

// Iteration 2: Steven Spielberg. The best? - How many drama movies did STEVEN SPIELBERG direct?
function howManyMovies(moviesArray) {}
function howManyMovies(moviesArray) {
return moviesArray.filter((element) => (element.director === "Steven Spielberg" && element.genre.includes("Drama"))).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;
return Math.round(moviesArray.filter((element) => element.score).reduce((acc, crr) => acc += crr.score, 0)/moviesArray.length*100)/100
}

// Iteration 4: Drama movies - Get the average of Drama Movies
function dramaMoviesScore(moviesArray) {}
function dramaMoviesScore(moviesArray) {
return scoresAverage(moviesArray.filter((element) => element.genre.includes("Drama")));
}

// Iteration 5: Ordering by year - Order by year, ascending (in growing order)
function orderByYear(moviesArray) {}
function orderByYear(moviesArray) {
moviesArrayCopy = moviesArray.slice();
return moviesArrayCopy.sort((a,b) => a.year - b.year || a.title.localeCompare(b.title));
}

// Iteration 6: Alphabetic Order - Order by title and print the first 20 titles
function orderAlphabetically(moviesArray) {}
function orderAlphabetically(moviesArray) {
moviesArrayCopy = moviesArray.slice();
moviesArrayCopy.sort((a,b) => a.title.localeCompare(b.title));
return moviesArrayCopy.map((element) => element.title).slice(0,20);
}

// BONUS - Iteration 7: Time Format - Turn duration of the movies from hours to minutes
function turnHoursToMinutes(moviesArray) {}
function turnHoursToMinutes(moviesArray) {
return moviesArray.map((element) => {
let hours = element.duration.match(/(\d+)(?=h)/);
let minutes = element.duration.match(/(\d+)(?=min)/);

let totalMinutes = 0;
if (hours) totalMinutes += parseInt(hours[0]) * 60;
if (minutes) totalMinutes += parseInt(minutes[0]);

return {
...element,
duration: totalMinutes
};
});
}


// BONUS - Iteration 8: Best yearly score average - Best yearly score average
function bestYearAvg(moviesArray) {}
function bestYearAvg(moviesArray) {
if (moviesArray.length <=0) return null;
allYears = [...new Set(moviesArray.map((movie) => movie.year))];
allYears.sort((a,b) => a-b)
currentHighestScore = -1;
currentHighestScoreYear = 0;
allYears.forEach((year) => {
currentYearScore = scoresAverage(moviesArray.filter((element) => year === element.year))
if (currentYearScore > currentHighestScore) {
currentHighestScore = currentYearScore;
currentHighestScoreYear = year;
}
})
return `The best year was ${currentHighestScoreYear} with an average score of ${currentHighestScore}`;
}

// console.log(bestYearAvg(movies));
68 changes: 68 additions & 0 deletions src/tempCodeRunnerFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const movies = require('./data.js');

// 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) {
return [...new Set(moviesArray.map((element) => element.director))]
}

// Iteration 2: Steven Spielberg. The best? - How many drama movies did STEVEN SPIELBERG direct?
function howManyMovies(moviesArray) {
return moviesArray.filter((element) => (element.director === "Steven Spielberg" && element.genre.includes("Drama"))).length
}

// Iteration 3: All scores average - Get the average of all scores with 2 decimals
function scoresAverage(moviesArray) {
if (moviesArray.length <= 0) return 0;
return Math.round(moviesArray.filter((element) => element.score).reduce((acc, crr) => acc += crr.score, 0)/moviesArray.length*100)/100
}

// Iteration 4: Drama movies - Get the average of Drama Movies
function dramaMoviesScore(moviesArray) {
return scoresAverage(moviesArray.filter((element) => element.genre.includes("Drama")));
}

// Iteration 5: Ordering by year - Order by year, ascending (in growing order)
function orderByYear(moviesArray) {
moviesArrayCopy = moviesArray.slice();
return moviesArrayCopy.sort((a,b) => a.year - b.year || a.title.localeCompare(b.title));
}

// Iteration 6: Alphabetic Order - Order by title and print the first 20 titles
function orderAlphabetically(moviesArray) {
moviesArrayCopy = moviesArray.slice();
moviesArrayCopy.sort((a,b) => a.title.localeCompare(b.title));
return moviesArrayCopy.map((element) => element.title).slice(0,20);
}

// BONUS - Iteration 7: Time Format - Turn duration of the movies from hours to minutes
function turnHoursToMinutes(moviesArray) {
return moviesArray.map((element) => {
let hours = element.duration.match(/(\d+)(?=h)/);
let minutes = element.duration.match(/(\d+)(?=min)/);

let totalMinutes = 0;
if (hours) totalMinutes += parseInt(hours[0]) * 60;
if (minutes) totalMinutes += parseInt(minutes[0]);

return {
...element,
duration: totalMinutes
};
});
}


// BONUS - Iteration 8: Best yearly score average - Best yearly score average
function bestYearAvg(moviesArray) {
allYears = [...new Set(moviesArray.map((movie) => movie.year))];
allYears.sort((a,b) => a-b)
bestAvgScorePerYearArray = [];
allYears.forEach((element) => {
bestAvgScorePerYearArray.push(`${element} with an average score of ${scoresAverage(moviesArray.filter((elementMoviesArray) => element.year === elementMoviesArray.year))}`);
});
return bestAvgScorePerYearArray;
}

console.log(bestYearAvg(movies));