Created
April 18, 2020 13:48
-
-
Save josecarneiro/919499af302ebe55102dbf03c1f79d56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint no-restricted-globals: 'off' */ | |
// 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 = (movies) => { | |
return movies.map((movie) => movie.director); | |
}; | |
// Iteration 2: Steven Spielberg. The best? - How many drama movies did STEVEN SPIELBERG direct? | |
const howManyMovies = (array) => { | |
return array.filter((movie) => { | |
const isDirectedByStevenSpielberg = movie.director === 'Steven Spielberg'; | |
const isDramaMovie = movie.genre.includes('Drama'); | |
return isDirectedByStevenSpielberg && isDramaMovie; | |
}).length; | |
}; | |
// Iteration 3: All rates average - Get the average of all rates with 2 decimals | |
const roundNumbers = (number, digits) => Math.round(number * 10 ** digits) / 10 ** digits; | |
const ratesAverage = (array) => { | |
if (array.length === 0) { | |
return 0; | |
} | |
const rates = array | |
.map((movie) => movie.rate) | |
// .filter(rate => typeof rate === 'number'); | |
.map((rate) => { | |
if (typeof rate === 'number') { | |
return rate; | |
} else { | |
return 0; | |
} | |
}); | |
const sumOfAllRates = rates.reduce((accumulator, rate) => accumulator + rate, 0); | |
const averageRate = sumOfAllRates / rates.length; | |
/* | |
const averageRate = rates.reduce((accumulator, rate) => accumulator + rate / rates.length, 0); | |
*/ | |
return roundNumbers(averageRate, 2); | |
}; | |
// Iteration 4: Drama movies - Get the average of Drama Movies | |
const dramaMoviesRate = (array) => { | |
const dramaMovies = array.filter((movie) => movie.genre.includes('Drama')); | |
const averageDramaMovieRate = ratesAverage(dramaMovies); | |
return averageDramaMovieRate; | |
}; | |
// Iteration 5: Ordering by year - Order by year, ascending (in growing order) | |
const orderByYear = (array) => { | |
const sortedMovies = [...array]; | |
// const sortedMovies = array.slice(); | |
sortedMovies.sort((firstMovie, secondMovie) => { | |
const firstMovieYearOfRelease = firstMovie.year; | |
const secondMovieYearOfRelease = secondMovie.year; | |
if (firstMovieYearOfRelease > secondMovieYearOfRelease) { | |
return 1; | |
} else if (firstMovieYearOfRelease < secondMovieYearOfRelease) { | |
return -1; | |
} else if (firstMovie.title.toLowerCase() > secondMovie.title.toLowerCase()) { | |
return 1; | |
} else { | |
return -1; | |
} | |
}); | |
return sortedMovies; | |
}; | |
// Iteration 6: Alphabetic Order - Order by title and print the first 20 titles | |
const orderAlphabetically = (array) => { | |
const sortedMovies = [...array]; | |
sortedMovies.sort((firstMovie, secondMovie) => { | |
return firstMovie.title.localeCompare(secondMovie.title); | |
}); | |
const movieTitles = sortedMovies.map((movie) => movie.title); | |
return movieTitles.slice(0, 20); | |
}; | |
// BONUS - Iteration 7: Time Format - Turn duration of the movies from hours to minutes | |
const turnHoursToMinutes = (array) => { | |
const moviesWithDurationAsMinutes = array.map((movie) => { | |
const durationAsString = movie.duration; | |
let duration = 0; | |
for (let value of durationAsString.split(' ')) { | |
const number = parseInt(value); | |
if (value.includes('h')) { | |
duration += number * 60; | |
} else if (value.includes('min')) { | |
duration += number; | |
} | |
} | |
const movieWithDurationInMinutes = { | |
...movie, | |
duration, | |
}; | |
return movieWithDurationInMinutes; | |
}); | |
return moviesWithDurationAsMinutes; | |
}; | |
// BONUS - Iteration 8: Best yearly rate average - Best yearly rate average | |
const bestYearAvg = (array) => { | |
if (array.length === 0) { | |
return null; | |
} | |
const movieAverageRateByYear = array.reduce((accumulator, movie) => { | |
const yearOfMovie = movie.year; | |
const rateOfMovie = movie.rate; | |
if (typeof accumulator[yearOfMovie] === 'undefined') { | |
accumulator[yearOfMovie] = [rateOfMovie]; | |
} else { | |
accumulator[yearOfMovie].push(rateOfMovie); | |
} | |
return accumulator; | |
}, {}); | |
for (let year in movieAverageRateByYear) { | |
const averageRateOfYear = movieAverageRateByYear[year].reduce( | |
(accumulator, rate, index, originalArray) => accumulator + rate / originalArray.length, | |
0 | |
); | |
movieAverageRateByYear[year] = averageRateOfYear; | |
} | |
const auxiliaryArray = Object.entries(movieAverageRateByYear); | |
auxiliaryArray.sort((a, b) => { | |
const rateOfYearA = a[1]; | |
const rateOfYearB = b[1]; | |
if (rateOfYearA > rateOfYearB) { | |
return -1; | |
} else { | |
return 1; | |
} | |
}); | |
const year = auxiliaryArray[0][0]; | |
const rate = auxiliaryArray[0][1]; | |
return `The best year was ${year} with an average rate of ${rate}`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment