From 7a2381709ce2c491a1622ed3dbb8e0213b7d0c3b Mon Sep 17 00:00:00 2001 From: marshalljordan1 Date: Fri, 17 Feb 2023 12:01:54 +0100 Subject: [PATCH] Add all exercises to Sprint 4 Create new clone and copied all files into Sprint 4 to hand in via pull request --- src/films.js | 79 +++++++++++++++++++++++++++++++++++++++----------- src/index.html | 6 ++-- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/films.js b/src/films.js index cdad23d..9ee7a10 100644 --- a/src/films.js +++ b/src/films.js @@ -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; } diff --git a/src/index.html b/src/index.html index 1f36d98..eac1f3f 100644 --- a/src/index.html +++ b/src/index.html @@ -1,10 +1,8 @@ - + + -

Sprint 4 IT ACADEMY

- - \ No newline at end of file