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

Mid-Module Assessment Diego Carrero #14

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
67 changes: 52 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Do not change the line below. If you'd like to run code from this file, you may use the `exampleMovies` variable below to gain access to an array of movies.
Do not change the line below. If you'd like to run code from this file, you may use the `exampleMovies`
variable below to gain access to an array of movies.

Keep in mind that your functions must still have and use a parameter for accepting all movies.
*/
Expand All @@ -9,7 +10,8 @@ const exampleMovies = require("./movies");
/**
* getAllMovieTitles()
* -----------------------------
* Returns all of titles from an array of movies. If the inputted `movies` array is empty, throw an error with a message.
* Returns all of titles from an array of movies. If the inputted `movies` array is empty, throw an
* error with a message.
* @param {Object[]} movies - An array of movies. See the `movies.js` file for an example of this array.
* @returns {string[]|Error} An array of strings, which are movie titles.
*
Expand All @@ -30,15 +32,21 @@ const exampleMovies = require("./movies");
"James and the Giant Peach",
];
*/
function getAllMovieTitles() {}
function getAllMovieTitles(movies) {
if (movies.length == 0) throw 'There are no movies!'
const titles = movies.map((movie) => movie.title);
return titles;
}

/**
* checkIfAnyMovieHasRating()
* -----------------------------
* Returns a boolean, representing whether or not any of the movies has been given the provided rating. If the inputted `movies` array is empty, throw an error with a message.
* Returns a boolean, representing whether or not any of the movies has been given the provided rating.
* If the inputted `movies` array is empty, throw an error with a message.
* @param {Object[]} movies - An array of movies. See the `movies.js` file for an example of this array.
* @param {string} [rating="G"] - A movie rating. Defaults to "G".
* @returns {boolean|Error} Returns `true` if a movie exists in the list with the given rating, otherwise returns `false`.
* @returns {boolean|Error} Returns `true` if a movie exists in the list with the given rating,
* otherwise returns `false`.
*
* NOTE: You must use the `.some()` method.
*
Expand All @@ -50,12 +58,16 @@ function getAllMovieTitles() {}
* checkIfAnyMovieHasRating(movies, "R");
* //> false
*/
function checkIfAnyMovieHasRating() {}
function checkIfAnyMovieHasRating(movies, rating = 'G') {
if (movies.length == 0) throw 'There are no movies!'
return movies.some((movie) => movie.rated == rating);
}

/**
* findById()
* -----------------------------
* Returns a movie object from an array of objects based on the ID. If the inputted `movies` array is empty, throw an error with a message. If the ID does not match any movie, return `null`.
* Returns a movie object from an array of objects based on the ID. If the inputted `movies` array is
* empty, throw an error with a message. If the ID does not match any movie, return `null`.
* @param {Object[]} movies - An array of movies. See the `movies.js` file for an example of this array.
* @param {string} id - A unique `imdbID`.
* @returns {Object|Error|null} The movie object with the matching `imdbID`.
Expand All @@ -68,15 +80,20 @@ function checkIfAnyMovieHasRating() {}
// Toy Story 4
};
*/
function findById() {}
function findById(movies, id) {
if (movies.length == 0) throw 'There are no movies!'
return movies.find((movie) => movie.imdbID == id) || null;
}

/**
* filterByGenre()
* -----------------------------
* Returns all movie objects with a matching genre. Case-insensitive. If the inputted `movies` array is empty, throw an error with a message. If no movies match the inputted `genre`, return `[]`.
* Returns all movie objects with a matching genre. Case-insensitive. If the inputted `movies` array is
* empty, throw an error with a message. If no movies match the inputted `genre`, return `[]`.
* @param {Object[]} movies - An array of movies. See the `movies.js` file for an example of this array.
* @param {string} genre - The genre of a movie. (e.g. "Fantasy")
* @returns {Object[]|Error} An array of movies where at least one of the genres matches the `genre` inputted.
* @returns {Object[]|Error} An array of movies where at least one of the genres matches the `genre`
* inputted.
*
* NOTE: You must use the `.filter()` method.
*
Expand All @@ -92,7 +109,14 @@ function findById() {}
* filterByGenre(movies, "Horror")
* //> []
*/
function filterByGenre() {}
function filterByGenre(movies, genre) {
if (movies.length == 0) throw 'There are no movies!'
genre = genre.toLowerCase();
return movies.filter((movie) => {
const genArr = movie.genre.toLowerCase().split(', ');
return genArr.includes(genre);
})
}

/**
* getAllMoviesReleasedAtOrBeforeYear()
Expand All @@ -118,7 +142,10 @@ function filterByGenre() {}
}
];
*/
function getAllMoviesReleasedAtOrBeforeYear() {}
function getAllMoviesReleasedAtOrBeforeYear(movies, year) {
if (movies.length == 0) throw 'There are no movies!'
return movies.filter((movie) => +movie.released.substr(7) <= year) || [];
}

/**
* checkMinMetascores()
Expand All @@ -134,12 +161,16 @@ function getAllMoviesReleasedAtOrBeforeYear() {}
* checkMinMetascores(movies, 90));
* //> false
*/
function checkMinMetascores() {}
function checkMinMetascores(movies, metascore) {
if (movies.length == 0) throw 'There are no movies!'
return movies.every((movie) => +movie.metascore > metascore);
}

/**
* getRottenTomatoesScoreByMovie()
* -----------------------------
* Transform each movie, returning an array of objects where the key is the title of the movie and the value is the score received from Rotten Tomatoes. If there are no movies, throw an error with a message.
* Transform each movie, returning an array of objects where the key is the title of the movie and the
* value is the score received from Rotten Tomatoes. If there are no movies, throw an error with a message.
* @param {Object[]} movies - An array of movies. See the `movies.js` file for an example of this array.
* @returns {Object[]|Error} An array of movie objects where the key is the movie title and the value is the score received from Rotten Tomatoes.
*
Expand All @@ -160,7 +191,13 @@ function checkMinMetascores() {}
{ "James and the Giant Peach": "91%" },
];
*/
function getRottenTomatoesScoreByMovie() {}
function getRottenTomatoesScoreByMovie(movies) {
if (movies.length == 0) throw 'There are no movies!'
return movies.map((movie) => {
const movRat = movie.ratings.find((rating) => rating.source == 'Rotten Tomatoes')
return {[movie.title]: movRat.value}
})
}

// Do not change anything below this line.
module.exports = {
Expand Down