Skip to content

Commit

Permalink
done again
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamindotdev committed Sep 27, 2024
1 parent b897c7f commit 7a48b78
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/movies.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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) => {
Expand All @@ -90,5 +85,3 @@ const bestYearAvg = (moviesArray) => {
? `The best year was ${bestYear} with an average score of ${bestAvg}`
: null;
};

console.log(bestYearAvg(movies));

0 comments on commit 7a48b78

Please sign in to comment.