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

PT_RMT_082024 Franziska Scheer #3859

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2001,3 +2001,5 @@ const movies = [
score: 8
}
];

module.exports = movies;
101 changes: 93 additions & 8 deletions src/movies.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,110 @@
const { uniqueSort } = require("jquery");
// const movies = require("./data.js");

// 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)?
function getAllDirectors(moviesArray) {}
function getAllDirectors(moviesArray) {
let allDirectors = moviesArray.map((e) => e.director);
let uniqueDirectors = [...new Set(allDirectors)];
return uniqueDirectors;
}


// Iteration 2: Steven Spielberg. The best? - How many drama movies did STEVEN SPIELBERG direct?
function howManyMovies(moviesArray) {}
function howManyMovies(moviesArray) {
return moviesArray.filter(
(e) => e.director === "Steven Spielberg" && e.genre.includes("Drama")
).length;
}

// Iteration 3: All scores average - Get the average of all scores with 2 decimals
function scoresAverage(moviesArray) {}
function scoresAverage(moviesArray) {
if (!moviesArray.length) return 0;
let sum = moviesArray.reduce((acc, curr) => {
if (isNaN(curr.score)) return acc;
return acc + curr.score;
}, 0);
average = sum / moviesArray.length;
average = Math.round(average * 100) / 100;
return average;
}

// Iteration 4: Drama movies - Get the average of Drama Movies
function dramaMoviesScore(moviesArray) {}
function dramaMoviesScore(moviesArray) {
let onlyDramas = moviesArray.filter((e) => e.genre.includes("Drama"));
return scoresAverage(onlyDramas);
}

// Iteration 5: Ordering by year - Order by year, ascending (in growing order)
function orderByYear(moviesArray) {}
function orderByYear(moviesArray) {
const clone = [...moviesArray];
clone.sort((a, b) => {
if (a.year === b.year) return a.title.localeCompare(b.title);
return a.year - b.year;
});
return clone;
}

// Iteration 6: Alphabetic Order - Order by title and print the first 20 titles
function orderAlphabetically(moviesArray) {}
function orderAlphabetically(moviesArray) {
const clone = [...moviesArray];
clone.sort((a, b) => a.title.localeCompare(b.title));
let count = 0;
const only20 = clone.filter((e) => {
if (count < 20) {
count++;
return true;
}
});
return only20.map((e) => e.title);
}

// BONUS - Iteration 7: Time Format - Turn duration of the movies from hours to minutes
function turnHoursToMinutes(moviesArray) {}
function turnHoursToMinutes(moviesArray) {
const newMovies = moviesArray.map((e) => {
let minutes = calcMinutes(e.duration);
e.duration = minutes;
return e;
});
return newMovies;
}

function calcMinutes(string) {
let hours = parseInt(string.slice(0, 1));
let minutes = 0;
if (string.length > 3) {
minutes = parseInt(string.slice(3, 5));
}
return hours * 60 + minutes;
}

// BONUS - Iteration 8: Best yearly score average - Best yearly score average
function bestYearAvg(moviesArray) {}
function bestYearAvg(moviesArray) {
if (!moviesArray.length) return null;
const yearsArr = moviesArray.map((e) => e.year);
uniqueYears = [...new Set(yearsArr)];
uniqueYears.sort((a, b) => a - b);

const avgScoresPerYear = uniqueYears.map((currYear) => {
let count = 0;
let sum = moviesArray.reduce((acc, curr) => {
if (curr.year === currYear) {
count++;
return acc + curr.score;
}
return acc;
}, 0);
return sum / count;
});
let max = 0;
let index = 0;
avgScoresPerYear.forEach((e, i) => {
if (e > max) {
max = e;
index = i;
}
});
return `The best year was ${uniqueYears[index]} with an average score of ${avgScoresPerYear[index]}`;
}

4 changes: 2 additions & 2 deletions tests/movies.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,11 @@ describe('Function "turnHoursToMinutes"', () => {
it('should be declared', () => {
expect(typeof turnHoursToMinutes).toBe('function');
});

it('should return an array', () => {
expect(turnHoursToMinutes(movies) instanceof Array).toBe(true);
});

it('should return a new array, not mutate the original one', () => {
const returnValue = turnHoursToMinutes(movies);
expect(returnValue instanceof Array).toBe(true);
Expand Down