diff --git a/index.js b/index.js index 9b48491..f465f06 100644 --- a/index.js +++ b/index.js @@ -12,30 +12,32 @@ console.log('πŸš€ It Works!'); * Good luck, have fun ! */ -// πŸ“ TODO: Number of filming locations +// 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(`There is ${getFilmingLocationsNumber()} filming locations in Paris`) -// πŸ“ TODO: Filming locations sorted by start date, from most recent to oldest. +// 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 sorted = filmingLocations.sort((a, b) => new Date(b.fields.date_debut) - new Date(a.fields.date_debut)) + return sorted } -console.log(``) +//console.log(sortFilmingLocationsByStartDate()[0]) -// πŸ“ TODO: Number of filming locations in 2020 only +// 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 result = filmingLocations.filter(location => new Date(location.fields.date_debut).getFullYear() == 2020); + return result.length } -console.log() +//console.log(getFilmingLocationsNumber2020()) -// πŸ“ TODO: Number of filming locations per year +// 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: // const filmingLocationsPerYear = { @@ -44,11 +46,16 @@ console.log() // } // 2. Log the result function getFilmingLocationsNumberPerYear () { - return {} + const filmingLocationsPerYear = {} + for(let element in filmingLocations){ + let year = new Date(filmingLocations[element].fields.date_debut).getFullYear() + filmingLocationsPerYear[year] = (filmingLocationsPerYear[year]+1) || 1 + } + return filmingLocationsPerYear } -console.log() +//console.log(getFilmingLocationsNumberPerYear()) -// πŸ“ TODO: Number of filming locations by district (arrondissement) +// 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: // const filmingLocationsPerDistrict = { @@ -57,42 +64,68 @@ console.log() // } // 2. Log the result function getFilmingLocationsNumberPerDistrict () { - return {} + const filmingLocationsPerDistrict = {} + for(let element in filmingLocations){ + let district = filmingLocations[element].fields.ardt_lieu + filmingLocationsPerDistrict[district] = (filmingLocationsPerDistrict[district]+1) || 1 + } + return filmingLocationsPerDistrict } -console.log() +//console.log(getFilmingLocationsNumberPerDistrict()) -// πŸ“ TODO: Number of locations per film, sorted in descending order +// 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 [] + var result = [] + var tournage= {} + for(let element in filmingLocations){ + let film = filmingLocations[element].fields.nom_tournage + let temp = result.find(element => element.film == film) + if(temp){ + temp.locations = temp.locations + 1 + } + else{ + tournage.film = film + tournage.locations = 1 + result.push({...tournage}) + } + } + result = result.sort((a, b) => b.locations - a.locations) + return result } -console.log() +//console.log(getFilmLocationsByFilm()[0]) +//console.log(getFilmLocationsByFilm()[getFilmLocationsByFilm().length-1]) -// πŸ“ TODO: Number of different films +// 7 πŸ“ TODO: Number of different films // 1. Implement the function // 2. Log the result function getNumberOfFilms() { - return '' + const result = new Set() + for(let element in filmingLocations){ + result.add(filmingLocations[element].fields.nom_tournage) + } + return result.size + } +//console.log(getNumberOfFilms()) -// πŸ“ TODO: All the filming locations of `LRDM - Patriot season 2` +// 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 [] + const result = filmingLocations.filter(element => element.fields.nom_tournage == 'LRDM - Patriot season 2').map(function(element){return element.fields.adresse_lieu}) + return result } +//console.log(getArseneFilmingLocations()) -// πŸ“ TODO: Tous les arrondissement des lieux de tournage de nos films favoris +// 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 // parameter. e.g. : // const films = { 'LRDM - Patriot season 2': ['75013'] } // 2. Log the result -function getFavoriteFilmsLocations (favoriteFilmsNames) { - return [] -} const favoriteFilms = [ 'LRDM - Patriot season 2', @@ -100,31 +133,82 @@ const favoriteFilms = 'Emily in Paris', ] -// πŸ“ TODO: All filming locations for each film +function getFavoriteFilmsLocations (favoriteFilmsNames) { + const films = [] + for (let j = 0 ; j < favoriteFilmsNames.length ; j++){ + let film = favoriteFilmsNames[j] + for(let i=0 ; i < filmingLocations.length ; i++) { + let temp = filmingLocations[i] + if (temp.fields.nom_tournage == film) { + let district = temp.fields.ardt_lieu + if(films[film]){ + films[film].push(district) + } + else{ + films[film]= [district] + } + } + } + } + return films +} +//console.log(getFavoriteFilmsLocations(favoriteFilms)) + +// 10 πŸ“ TODO: All filming locations for each film // e.g. : // const films = { // 'LRDM - Patriot season 2': [{...}], // 'Une jeune fille qui va bien': [{...}] // } function getFilmingLocationsPerFilm () { - return { } + const res = {} + for (let i = 0 ; i < filmingLocations.length ; i++){ + let film=filmingLocations[i].fields.nom_tournage + let location=filmingLocations[i].fields.adresse_lieu + if(res[film]){ + res[film].push(location) + } + else{ + res[film]= [location] + } + } + return res } +//console.log(getFilmingLocationsPerFilm()['LRDM - Patriot season 2']) -// πŸ“ TODO: Count each type of film (Long mΓ©trage, SΓ©rie TV, etc...) +// 11 πŸ“ TODO: Count each type of film (Long mΓ©trage, SΓ©rie TV, etc...) // 1. Implement the function // 2. Log the result function countFilmingTypes () { - return {} + const res = {} + for(let element in filmingLocations){ + let type = filmingLocations[element].fields.type_tournage + res[type] = (res[type]+1) || 1 ; + } + return res } +//console.log(countFilmingTypes()) -// πŸ“ TODO: Sort each type of filming by count, from highest to lowest +// 12 πŸ“ TODO: Sort each type of filming by count, from highest to lowest // 1. Implement the function. It should return a sorted array of objects like: // [{type: 'Long mΓ©trage', count: 1234}, {...}] // 2. Log the result -function sortedCountFilmingTypes () { - return [] +function sortedCountFilmingTypes (count) { + let res = [] + for(let element in filmingLocations){ + let typetournage = filmingLocations[element].fields.type_tournage + let temp = res.find(el => el.type == typetournage) + if(temp){ + temp.count +=1 + } + else{ + res.push({type : typetournage, count : 1}) + } + } + res = res.sort((a,b) => b.count - a.count) + return res } - +//console.log(sortedCountFilmingTypes()) /** * This arrow functions takes a duration in milliseconds and returns a * human-readable string of the duration @@ -133,10 +217,35 @@ function sortedCountFilmingTypes () { */ const duration = (ms) => `${(ms/(1000*60*60*24)).toFixed(0)} days, ${((ms/(1000*60*60))%24).toFixed(0)} hours and ${((ms/(1000*60))%60).toFixed(0)} minutes` -// πŸ“ TODO: Find the filming location with the longest duration +// 13 πŸ“ TODO: Find the filming location with the longest duration // 1. Implement the function // 2. Log the filming location, and its computed duration +function LongestDuration(){ + let max = new Date(filmingLocations[0].fields.date_fin) - new Date(filmingLocations[0].fields.date_debut) + for(let i=1; i< filmingLocations.length; i++){ + const duree = new Date(filmingLocations[i].fields.date_fin) - new Date(filmingLocations[i].fields.date_debut) + if(duree > max){ + max = duree + } + } + return duration(max) +} +//console.log(LongestDuration()) -// πŸ“ TODO: Compute the average filming duration +// 14 πŸ“ TODO: Compute the average filming duration // 1. Implement the function // 2. Log the result +function mean(array) { + let i = 0 + let summ = 0 + const len = array.length + while (i < len) { + summ = summ + array[i++] + } + return summ / len +} +function AverageDuration(){ + const allduration = filmingLocations.map(function (element){ return new Date(element.fields.date_fin) - new Date(element.fields.date_debut)}) + return duration(mean(allduration)) +} +console.log(AverageDuration()) \ No newline at end of file