Skip to content

Instantly share code, notes, and snippets.

@ross-u
Last active October 28, 2019 08:29
Show Gist options
  • Save ross-u/a94379525394822dab3b9dee8f0a0686 to your computer and use it in GitHub Desktop.
Save ross-u/a94379525394822dab3b9dee8f0a0686 to your computer and use it in GitHub Desktop.
LAB SOLUTION - javascript-all-time-movies
// HELPER FUNCTIONS
function convertHoursToMinutes(hours) {
return hours * 60;
}
function isAnEmptyArray(array) {
return array.length === 0;
}
//
// LAB ITERATIONS:
//
// Turn duration of the movies from hours to minutes
function turnHoursToMinutes(moviesArr) {
var updatedMovies = moviesArr.map(function(movie) {
var movieCopy = Object.assign({}, movie); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
var timeInMinutes = 0;
if (typeof movie.duration === 'number') {
timeInMinutes = movie.duration;
} else {
var timeArray = movie.duration.split(' ');
var timeInMinutes = 0;
timeArray.forEach(function(timeString) {
var hours;
if (timeString.includes('h')) {
hours = parseInt(timeString);
var convertedMinutes = convertHoursToMinutes(hours);
timeInMinutes += convertedMinutes;
} else if (timeString.includes('m')) {
minutes = parseInt(timeString);
timeInMinutes += minutes;
}
});
}
movieCopy.duration = timeInMinutes;
return movieCopy;
});
return updatedMovies;
}
// Get the average of all rates with 2 decimals
function ratesAverage(moviesArr) {
var numberOfMovies = moviesArr.length;
var totalRate = moviesArr.reduce(function(totalRate, movie) {
var updatedTotalRate = totalRate + movie.rate;
return updatedTotalRate;
}, 0);
var averageRate = totalRate / numberOfMovies;
var roundedRateString = averageRate.toFixed(2); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
var roundedRateFloat = parseFloat(roundedRateString); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat
return roundedRateFloat;
}
//
// Get the average of Drama Movies
function dramaMoviesRate(moviesArr) {
var dramaMovies = moviesArr.filter(function(movie) {
return movie.genre.includes('Drama');
});
if (dramaMovies.length === 0) {
return undefined;
} else {
var dramaRatesAverage = ratesAverage(dramaMovies);
return dramaRatesAverage;
}
}
// Order by time duration, in growing order
function orderByDuration(moviesArr) {
var normalizedMovies = turnHoursToMinutes(moviesArr);
normalizedMovies.sort(function(a, b) {
if (a.duration < b.duration) {
return -1;
} else if (a.duration > b.duration) {
return 1;
} else if (a.duration === b.duration) {
if (a.title < b.title) {
return -1;
} else if (a.title > b.title) {
return 1;
}
return 0;
}
});
var moviesOrdered = [...normalizedMovies];
return moviesOrdered;
}
// How many movies did STEVEN SPIELBERG
function howManyMovies(moviesArr) {
if (isAnEmptyArray(moviesArr)) {
return undefined;
}
var dramaMoviesCount = moviesArr.reduce(function(dramaMovies, movie) {
if (
movie.genre.includes('Drama') &&
movie.director === 'Steven Spielberg'
) {
var updatedDramaMovies = dramaMovies + 1;
return updatedDramaMovies;
}
return dramaMovies;
}, 0);
return `Steven Spielberg directed ${dramaMoviesCount} drama movies!`;
}
// Order by title and print the first 20 titles
function orderAlphabetically(moviesArr) {
moviesArr.sort(function(a, b) {
if (a.title > b.title) {
return 1;
}
if (a.title < b.title) {
return -1;
}
return 0;
});
var allMovieTitles = moviesArr.map(function(movie) {
return movie.title;
});
if (allMovieTitles.length > 20) {
var frist20Titles = allMovieTitles.slice(0, 20);
return frist20Titles;
}
return allMovieTitles;
}
// Best yearly rate average
function bestYearAvg(moviesArr) {
if (isAnEmptyArray(moviesArr)) {
return undefined;
}
var moviesDataset = {};
// Create an object for each year, with the year as a property name.
// Object includes total of movies and rating for that year, example:
// 2019: { year: 2019, moviesCount: 1 , totalOfAllRatings: 7 }
moviesArr.forEach(function(movie) {
if (!moviesDataset[movie.year]) {
var newEntry = {
year: parseInt(movie.year),
moviesCount: 1,
totalOfAllRatings: movie.rate,
};
moviesDataset[movie.year] = newEntry;
} else {
moviesDataset[movie.year].moviesCount += 1;
moviesDataset[movie.year].totalOfAllRatings += movie.rate;
}
});
// Update the moviesDataset to create avgRate property on each object
for (var year in moviesDataset) {
var { moviesCount, totalOfAllRatings } = moviesDataset[year];
var averageRate = totalOfAllRatings / moviesCount;
moviesDataset[year].avgRate = averageRate;
}
var bestYear = new Date().getFullYear();
var bestAvgRate = 0;
// Iterate over the moviesDataset object to find the year with the best avgRate
for (var year in moviesDataset) {
var dataPoint = moviesDataset[year];
if (dataPoint.avgRate > bestAvgRate) {
bestYear = dataPoint.year;
bestAvgRate = dataPoint.avgRate;
} else if (dataPoint.avgRate === bestAvgRate) {
if (dataPoint.year < bestYear) {
bestYear = dataPoint.year;
bestAvgRate = dataPoint.avgRate;
}
}
}
bestAvgRate = parseFloat(bestAvgRate.toFixed(1));
return `The best year was ${bestYear} with an average rate of ${bestAvgRate}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment