diff --git a/index.js b/index.js index 9b48491..7791310 100644 --- a/index.js +++ b/index.js @@ -12,29 +12,47 @@ console.log('πŸš€ It Works!'); * Good luck, have fun ! */ +//Question 1 // πŸ“ TODO: Number of filming locations // 1. Make the function return the number of filming locations function getFilmingLocationsNumber () { - return '' + return filmingLocations.length } -console.log(`There is ${getFilmingLocationsNumber()} filming locations in Paris`) +console.log("\n", "Question 1") +console.log(`There is ${getFilmingLocationsNumber()} filming locations in Paris.`) +//Question 2 // πŸ“ TODO: Filming locations sorted by start date, from most recent to oldest. // 1. Implement the function // 2. Log the first and last item in array function sortFilmingLocationsByStartDate () { - return '' + //const filmingLocationsByStartDate2 = filmingLocations.sort(function (film) { return film["date_debut"]}) + const filmingLocationsByStartDate = (film1, film2) => new Date(film1.fields.date_debut) - new Date(film2.fields.date_debut) + const sortedArray = filmingLocations.sort(filmingLocationsByStartDate) + return sortedArray } -console.log(``) +console.log("\n", "Question 2") +console.log("The oldest movie to start is:", "\n", sortFilmingLocationsByStartDate()[0]) +console.log("The most recent movie to start is:", "\n", sortFilmingLocationsByStartDate()[filmingLocations.length-1]) +//Question 3 // πŸ“ TODO: Number of filming locations in 2020 only // 1. Make the function return the number of filming locations in 2020 only // 2. Log the result function getFilmingLocationsNumber2020 () { - return '' + const filmingLocations2020 = [] //Array + for (let pas = 0; pas < filmingLocations.length; pas++) { + if (filmingLocations[pas].fields.annee_tournage === "2020") { + filmingLocations2020.push(filmingLocations[pas].fields.nom_tournage) + } + } + return filmingLocations2020 } -console.log() +console.log("\n", "Question 3") +console.log(`There are ${getFilmingLocationsNumber2020().length} filming locations in 2020`) +//filmingLocations.filter(function filmingLocations => {return filmingLocations.fields.nom_tournage === "2020"}) +//Question 4 // πŸ“ TODO: Number of filming locations per year // 1. Implement the function, the expected result is an object with years as // keys and filming locations number as value, e.g: @@ -44,10 +62,23 @@ console.log() // } // 2. Log the result function getFilmingLocationsNumberPerYear () { - return {} + let filmingLocationsPerYear = { + '2016' : 0, + '2017' : 0, + '2018' : 0, + '2019' : 0, + '2020' : 0, + '2021' : 0 + } + for (let pas = 0; pas < filmingLocations.length; pas++) { + filmingLocationsPerYear[filmingLocations[pas].fields.annee_tournage] = filmingLocationsPerYear[filmingLocations[pas].fields.annee_tournage] + 1 + } + return {filmingLocationsPerYear} } -console.log() +console.log("\n", "Question 4") +console.log(getFilmingLocationsNumberPerYear()) +//Question 5 // πŸ“ TODO: Number of filming locations by district (arrondissement) // 1. Implement the function, the expected result is an object with // district as keys and filming locations number as value, e.g: @@ -57,33 +88,90 @@ console.log() // } // 2. Log the result function getFilmingLocationsNumberPerDistrict () { - return {} + let filmingLocationsPerDistrict = {} + for (let pas = 0; pas < filmingLocations.length; pas++) { + if(filmingLocationsPerDistrict[filmingLocations[pas].fields.ardt_lieu] === undefined) { + filmingLocationsPerDistrict[filmingLocations[pas].fields.ardt_lieu] = 1 + } + else { + filmingLocationsPerDistrict[filmingLocations[pas].fields.ardt_lieu] = filmingLocationsPerDistrict[filmingLocations[pas].fields.ardt_lieu] + 1 + } + } + return {filmingLocationsPerDistrict} } -console.log() +console.log("\n", "Question 5") +console.log(getFilmingLocationsNumberPerDistrict()) +//console.log(Object.keys(locationsPerDistrict).map((district) => ({'arrondissement': district, 'count': locationsPerDistrict[district]})).sort((a,b) => b.count-a.count)) +//Question 6 // πŸ“ TODO: Number of locations per film, sorted in descending order // 1. Implement the function, result expected as an array of object like: // const result = [{film: 'LRDM - Patriot season 2', locations: 12}, {...}] // 2. Log the first and last item of the array function getFilmLocationsByFilm () { - return [] + let filmingLocationsByFilm = [] + let result = false + let indice = 0 + for (let x = 0; x < filmingLocations.length; x++) { + for (let pas = 0; pas < filmingLocationsByFilm.length; pas++) { + if (filmingLocationsByFilm[pas].film === filmingLocations[x].fields.nom_tournage ) { + result = true + indice = pas + } + } + if (result == true) { + filmingLocationsByFilm[indice].locations = filmingLocationsByFilm[indice].locations + 1 + result = false + } + else { + let movie = { + film : filmingLocations[x].fields.nom_tournage, + locations : 1 + } + filmingLocationsByFilm.push(movie) + } + } + return filmingLocationsByFilm } -console.log() +console.log("\n", "Question 6") +console.log(getFilmLocationsByFilm().sort((a,b) => a.locations - b.locations).reverse()) + +//Question 7 // πŸ“ TODO: Number of different films // 1. Implement the function // 2. Log the result function getNumberOfFilms() { - return '' + let listFilms = [] + for (let i = 0; i < filmingLocations.length; i++) { + if (listFilms.indexOf(filmingLocations[i].fields.nom_tournage) === -1) { + listFilms.push(filmingLocations[i].fields.nom_tournage) + } + } + return listFilms } +console.log("\n", "Question 7") +console.log("There are",getNumberOfFilms().length, "different films.") +//Question 8 // πŸ“ TODO: All the filming locations of `LRDM - Patriot season 2` // 1. Return an array with all filming locations of LRDM - Patriot season 2 // 2. Log the result function getArseneFilmingLocations () { - return [] + let ArseneFilmingLocations = [] + for (let i=0; i < filmingLocations.length; i++) { + if(filmingLocations[i].fields.nom_tournage === 'LRDM - Patriot season 2') { + if(ArseneFilmingLocations.indexOf(filmingLocations[i].fields.adresse_lieu) === -1) { + ArseneFilmingLocations.push(filmingLocations[i].fields.adresse_lieu) + } + } + } + return ArseneFilmingLocations } +console.log("\n", "Question 8") +console.log("The filming locations of `LRDM - Patriot season 2` are : \n", getArseneFilmingLocations()) +//Question 9 // πŸ“ TODO: Tous les arrondissement des lieux de tournage de nos films favoris // (favoriteFilms) // 1. Return an array of all the districts of each favorite films given as a @@ -91,7 +179,22 @@ function getArseneFilmingLocations () { // const films = { 'LRDM - Patriot season 2': ['75013'] } // 2. Log the result function getFavoriteFilmsLocations (favoriteFilmsNames) { - return [] + let filmArray = [] + for (let i = 0; i < favoriteFilmsNames.length; i++) { + let districtArray = [] + for (let j = 0; j < filmingLocations.length; j++) { + if(filmingLocations[j].fields.nom_tournage === favoriteFilmsNames[i]) { + if(districtArray.indexOf(filmingLocations[j].fields.ardt_lieu) === -1) { + districtArray.push(filmingLocations[j].fields.ardt_lieu) + } + } + } + let films = { + [favoriteFilmsNames[i]] : districtArray + } + filmArray.push(films) + } + return filmArray } const favoriteFilms = [ @@ -99,7 +202,10 @@ const favoriteFilms = 'Alice NEVERS', 'Emily in Paris', ] +console.log("\n", "Question 9") +//console.log(getFavoriteFilmsLocations(favoriteFilms)) +//Question 10 // πŸ“ TODO: All filming locations for each film // e.g. : // const films = { @@ -107,23 +213,66 @@ const favoriteFilms = // 'Une jeune fille qui va bien': [{...}] // } function getFilmingLocationsPerFilm () { - return { } + const films = {} + let filmsList = [] + for (let i = 0; i `${(ms/(1000*60*60*24)).toFixed(0)} days, ${((ms/(1000*60*60))%24).toFixed(0)} hours and ${((ms/(1000*60))%60).toFixed(0)} minutes` +//Question 13 // πŸ“ TODO: Find the filming location with the longest duration // 1. Implement the function // 2. Log the filming location, and its computed duration +console.log("\n", "Question 13") +let longestDuration = (a,b) => (new Date(a.fields.date_fin).getTime()-new Date(a.fields.date_debut).getTime()) - (new Date(b.fields.date_fin)-new Date(b.fields.date_debut)) +const sortedA = filmingLocations.sort(longestDuration).reverse() +console.log(sortedA[0]) +//Question 14 // πŸ“ TODO: Compute the average filming duration // 1. Implement the function // 2. Log the result +console.log("\n", "Question 14") +let sum = 0 +for (let i = 0; i < filmingLocations.length; i++) { + sum = sum + new Date(filmingLocations[i].fields.date_fin).getTime()-new Date(filmingLocations[i].fields.date_debut).getTime() +} +let average = duration(sum / filmingLocations.length) +console.log(average) +