Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save josecarneiro/c13ccf59d8aa6dfee5d3a5a1998ae89a to your computer and use it in GitHub Desktop.
Save josecarneiro/c13ccf59d8aa6dfee5d3a5a1998ae89a to your computer and use it in GitHub Desktop.
/* eslint no-restricted-globals: 'off' */
// Iteration 1: 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 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: 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);
};
/*
const orderAlphabetically = array => {
const movieTitles = array.map(movie => movie.title);
movieTitles.sort((firstMovieTitle, secondMovieTitle) => {
return firstMovieTitle.localeCompare(secondMovieTitle);
});
return movieTitles.slice(0, 20);
};
*/
// Iteration 4: 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 5: 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 6: 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: 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