From 520084d2acef825ecce5376a27d227a3cdb70062 Mon Sep 17 00:00:00 2001 From: Pierre-Brieuc <73168906+Pierre-Brieuc@users.noreply.github.com> Date: Mon, 19 Sep 2022 17:12:18 +0200 Subject: [PATCH 1/2] latest version --- index.js | 234 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 216 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 9b48491..feb71d5 100644 --- a/index.js +++ b/index.js @@ -12,29 +12,64 @@ console.log('πŸš€ It Works!'); * Good luck, have fun ! */ + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ TODO: Number of filming locations // 1. Make the function return the number of filming locations function getFilmingLocationsNumber () { - return '' + return filmingLocations.length } +console.log("\n\n//////////////////////////////////////////////////////////////////") console.log(`There is ${getFilmingLocationsNumber()} filming locations in Paris`) + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ 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 comparison = (elemA, elemB) => new Date(elemB["fields"]["date_debut"]) - new Date(elemA["fields"]["date_debut"]) + let sortedArray = filmingLocations.sort(comparison) + return sortedArray; } -console.log(``) +console.log("\n\n//////////////////////////////////////////////////////////////////") +let temparray = sortFilmingLocationsByStartDate() +console.log(temparray[0],temparray[temparray.length-1]) + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ 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 '' + let counter = 0; + const temp = [] + for (let i=0; i x == filmingLocations[i]["fields"]["adresse_lieu"])))) { + temp[temp.length] = filmingLocations[i]["fields"]["adresse_lieu"] + counter = counter + 1; + } + } + return counter } -console.log() +console.log("\n\n//////////////////////////////////////////////////////////////////") +console.log("The number of filming locations in 2020 is ",getFilmingLocationsNumber2020()) + + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ 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 +79,24 @@ console.log() // } // 2. Log the result function getFilmingLocationsNumberPerYear () { - return {} + const filmingLocationsPerYear = {} + for (const elem of filmingLocations){ + if (!(elem["fields"]["annee_tournage"] in filmingLocationsPerYear)) { + filmingLocationsPerYear[elem["fields"]["annee_tournage"]] = 1 + } else { + filmingLocationsPerYear[elem["fields"]["annee_tournage"]] += 1 + } + } + return filmingLocationsPerYear } -console.log() +console.log("\n\n//////////////////////////////////////////////////////////////////") +console.log(getFilmingLocationsNumberPerYear()) + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ 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,41 +106,125 @@ console.log() // } // 2. Log the result function getFilmingLocationsNumberPerDistrict () { - return {} + const filmingLocationsPerDistrict = {} + const temp = [] + for (const film of filmingLocations){ + if (!(film["fields"]["ardt_lieu"] in filmingLocationsPerDistrict)) { + filmingLocationsPerDistrict[film["fields"]["ardt_lieu"]] = 1 + temp[temp.length] = film["fields"]["adresse_lieu"] + } else if (!(film["fields"]["adresse_lieu"] in temp)) { + filmingLocationsPerDistrict[film["fields"]["ardt_lieu"]] += 1 + temp[temp.length] = film["fields"]["adresse_lieu"] + } + } + return filmingLocationsPerDistrict } -console.log() +console.log("\n\n//////////////////////////////////////////////////////////////////") +console.log(getFilmingLocationsNumberPerDistrict()) + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ 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 [] + const result = [] + for (const film of filmingLocations){ + //if (!(result.find(x => x == film["fields"]["nom_tournage"]))) { + // result[result.lengt] = {film: film["fields"]["nom_tournage"], locations: 1} + //} else { + // console.log("yes") + // result[0][locations] += 1 + //} + + let bool = false + if (result.length == 0) { + result[result.length] = {'film':film["fields"]["nom_tournage"], 'locations':1} + } else { + for (const elem of result){ + if (elem['film'] == film["fields"]["nom_tournage"]) { + elem['locations'] += 1 + bool = true + } + } + if (!bool) { + result[result.length] = {'film':film["fields"]["nom_tournage"], 'locations':1} + bool = false + } + } + } + const condition = (a,b) => b['locations'] - a['locations'] + result.sort(condition) + return result } -console.log() +console.log("\n\n//////////////////////////////////////////////////////////////////") +console.log(getFilmLocationsByFilm()) + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ TODO: Number of different films // 1. Implement the function // 2. Log the result function getNumberOfFilms() { - return '' + return getFilmLocationsByFilm().length.toString() } +//console.log('The number of different films is ',getNumberOfFilms(),'.') + + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ 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 PatriotLocations = [] + for (const film of filmingLocations) { + if ((film["fields"]["nom_tournage"] == "LRDM - Patriot season 2") && (!(film["fields"]["adresse_lieu"] in PatriotLocations))) { + PatriotLocations[PatriotLocations.length] = film["fields"]["adresse_lieu"] + } + } + return PatriotLocations } +//console.log(getArseneFilmingLocations()) + + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ 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 [] +function getFavoriteFilmsLocations (favoriteFilms) { + const films = {} + for (const f of filmingLocations) { + if (favoriteFilms.find(fav => fav == f["fields"]["nom_tournage"])) { + let nom_tournage = f["fields"]["nom_tournage"] + let arr_lieu = f["fields"]["ardt_lieu"] + + if (!(nom_tournage in films)) { + films[nom_tournage] = [] + films[nom_tournage][films[nom_tournage].length] = arr_lieu + } else if (!(films[nom_tournage].find(x => x == arr_lieu))) { + films[nom_tournage][films[nom_tournage].length] = arr_lieu + } + } + } + return films } const favoriteFilms = [ @@ -100,6 +233,13 @@ const favoriteFilms = 'Emily in Paris', ] +//console.log(getFavoriteFilmsLocations(favoriteFilms)) + + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ TODO: All filming locations for each film // e.g. : // const films = { @@ -107,24 +247,71 @@ const favoriteFilms = // 'Une jeune fille qui va bien': [{...}] // } function getFilmingLocationsPerFilm () { - return { } + const films = {} + for (const f of filmingLocations) { + let nom_tournage = f["fields"]["nom_tournage"] + let loc = f["fields"]["adresse_lieu"] + + if (!(nom_tournage in films)) { + films[nom_tournage] = [] + films[nom_tournage][films[nom_tournage].length] = loc + } else if (!(films[nom_tournage].find(x => x == loc))) { + films[nom_tournage][films[nom_tournage].length] = loc + } + } + return films } +//console.log(getFilmingLocationsPerFilm()) + + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ 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 typesOfFilm = {} + for (const f of filmingLocations) { + let type_tournage = f["fields"]["type_tournage"] + if (!(type_tournage in typesOfFilm)) { + typesOfFilm[type_tournage] = 1 + } else { + typesOfFilm[type_tournage] += 1 + } + } + return typesOfFilm } +//console.log(countFilmingTypes()) + + + + + +///////////////////////////////////////////////////////////////////////////////// // πŸ“ 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 [] + const countTypesOfFilm = [] + const typesOfFilm = countFilmingTypes() + for (const t in typesOfFilm) { + countTypesOfFilm[countTypesOfFilm.length] = {type: t, count: typesOfFilm[t]} + } + return countTypesOfFilm } +//console.log(sortedCountFilmingTypes()) + + + + + +///////////////////////////////////////////////////////////////////////////////// /** * This arrow functions takes a duration in milliseconds and returns a * human-readable string of the duration @@ -136,7 +323,18 @@ const duration = (ms) => `${(ms/(1000*60*60*24)).toFixed(0)} days, ${((ms/(1000* // πŸ“ TODO: Find the filming location with the longest duration // 1. Implement the function // 2. Log the filming location, and its computed duration +function filmingLocationWithTheLongestDuration () { + let longestDuration = filmingLocations[0] + for (const film of filmingLocations) { + + } + return longestDuration +} +//console.log(filmingLocationWithTheLongestDuration()) // πŸ“ TODO: Compute the average filming duration // 1. Implement the function // 2. Log the result +function averageFilmingDuration () { + return +} From 7d75e568c8a4d8b7c1259e93e412f263b9570252 Mon Sep 17 00:00:00 2001 From: Pierre-Brieuc <73168906+Pierre-Brieuc@users.noreply.github.com> Date: Thu, 22 Sep 2022 10:16:18 +0200 Subject: [PATCH 2/2] new version --- index.js | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index feb71d5..ea0caa2 100644 --- a/index.js +++ b/index.js @@ -176,7 +176,7 @@ function getNumberOfFilms() { return getFilmLocationsByFilm().length.toString() } -//console.log('The number of different films is ',getNumberOfFilms(),'.') +console.log('The number of different films is ',getNumberOfFilms(),'.') @@ -196,7 +196,7 @@ function getArseneFilmingLocations () { return PatriotLocations } -//console.log(getArseneFilmingLocations()) +console.log(getArseneFilmingLocations()) @@ -233,7 +233,7 @@ const favoriteFilms = 'Emily in Paris', ] -//console.log(getFavoriteFilmsLocations(favoriteFilms)) +console.log(getFavoriteFilmsLocations(favoriteFilms)) @@ -262,7 +262,7 @@ function getFilmingLocationsPerFilm () { return films } -//console.log(getFilmingLocationsPerFilm()) +console.log(getFilmingLocationsPerFilm()) @@ -285,7 +285,7 @@ function countFilmingTypes () { return typesOfFilm } -//console.log(countFilmingTypes()) +console.log(countFilmingTypes()) @@ -305,7 +305,7 @@ function sortedCountFilmingTypes () { return countTypesOfFilm } -//console.log(sortedCountFilmingTypes()) +console.log("\n\n",sortedCountFilmingTypes()) @@ -324,17 +324,34 @@ const duration = (ms) => `${(ms/(1000*60*60*24)).toFixed(0)} days, ${((ms/(1000* // 1. Implement the function // 2. Log the filming location, and its computed duration function filmingLocationWithTheLongestDuration () { - let longestDuration = filmingLocations[0] + let duration = Date.parse(filmingLocations[0]["fields"]["date_fin"]) - Date.parse(filmingLocations[0]["fields"]["date_debut"]) + let longest = {"name": filmingLocations[0], "duration": duration} for (const film of filmingLocations) { - + duration = Date.parse(film["fields"]["date_fin"]) - Date.parse(film["fields"]["date_debut"]) + if (duration > longest["duration"]){ + longest["name"] = film["fields"]["nom_tournage"] + longest["duration"] = duration + } } - return longestDuration + return longest["name"] } -//console.log(filmingLocationWithTheLongestDuration()) +console.log("The filming location with the longest duration is",filmingLocationWithTheLongestDuration()) // πŸ“ TODO: Compute the average filming duration // 1. Implement the function // 2. Log the result function averageFilmingDuration () { - return + let sum = 0 + let duration = 0 + for (const film of filmingLocations) { + duration = Date.parse(film["fields"]["date_fin"]) - Date.parse(film["fields"]["date_debut"]) + sum += duration + } + return sum / filmingLocations.length +} +console.log("The average filming duration is",averageFilmingDuration()) + +function bis () { + return filmingLocations.reduce((pre, curr) => pre + curr, 0) } +//console.log("The average filming duration is",bis()) \ No newline at end of file