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

Add all exercises to Sprint 4 #18

Open
wants to merge 1 commit into
base: main
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
79 changes: 62 additions & 17 deletions src/films.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,88 @@
// Exercise 1: Get the array of all directors.
function getAllDirectors(array) {
let result = ???;
console.log("EXERCICE 1 ->", result);
return result;
let allDirectors = array.map((film) => film.director);
return allDirectors;
}

// Exercise 2: Get the films of a certain director
function getMoviesFromDirector(array, director) {

let moviesFromDirector = array.filter((film) => film.director === director);
return moviesFromDirector;
}

// Exercise 3: Calculate the average of the films of a given director.
function moviesAverageOfDirector(array, director) {

let moviesFromDirector = array.filter((film) => film.director === director);
const averageScore = moviesFromDirector.reduce((acc, cur) => acc + cur.score, 0)
return averageScore / moviesFromDirector.length.toFixed(2)
}

// Exercise 4: Alphabetic order by title
// Exercise 4: Alphabetic order by title
function orderAlphabetically(array) {

var firstTwenty = [...array].sort((a,b) => {
if (a.title < b.title) return -1;
return 1;
});
firstTwenty = firstTwenty.map((film) => film.title);
if (firstTwenty.length > 20) {
//console.log(firstTwenty.slice(0,20));
return firstTwenty.slice(0,20);
} else {
return firstTwenty;
}
}

// Exercise 5: Order by year, ascending
function orderByYear() {

function orderByYear(array) {
let byYear = [...array].sort((a,b) => {
if (a.year === b.year) {
if (a.title < b.title) return -1;
return 1;
} else {
return a.year - b.year;
}
});
return byYear;
}

// Exercise 6: Calculate the average of the movies in a category
function moviesAverageByCategory() {

}

function moviesAverageByCategory(array, category) {
let categories = array.filter((film) => film.genre == category);
const catAvg = categories.reduce((acc, cur) => acc + cur.score, 0);
let count = 0;
for (let category of categories) {
if (category.score !== null && category.score !== '') {
count++;
}
}
return catAvg / count.toFixed(2);
}

// Exercise 7: Modify the duration of movies to minutes
function hoursToMinutes() {

function hoursToMinutes(array) {
let moviesInMinutes = array.map((film) => {
const durationParts = film.duration.split(' ');
const hoursToMinutes = parseInt(durationParts[0]) * 60;
const minutes = parseInt(durationParts[1]);
let totalMinutes;
if (hoursToMinutes === 0)
totalMinutes = minutes;
else if (isNaN(minutes))
totalMinutes = hoursToMinutes;
else
totalMinutes = hoursToMinutes + minutes;
return {...film, duration: totalMinutes};
});
return moviesInMinutes;
}

// Exercise 8: Get the best film of a year
function bestFilmOfYear() {

function bestFilmOfYear(array, year) {
let filmYear = array.filter((film) => film.year === year);
const highScore = filmYear.sort((a, b) => { a.score - b.score});
let bestFilm = [];
bestFilm.push(highScore[0]);
return bestFilm;
}


Expand Down
6 changes: 2 additions & 4 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<!DOCTYPE html>
<html>

<head>
</head>
<body>

<h1>Sprint 4 IT ACADEMY</h1>

</body>

</html>