diff --git a/Archived_README/README.md b/Archived_README/README.md new file mode 100644 index 0000000..0e30b9c --- /dev/null +++ b/Archived_README/README.md @@ -0,0 +1,85 @@ +# Manipulate Data with Javascript and NodeJS + +## 🌟 Goal + +> Learning Javascript basics by manipulating arrays, objects, functions etc... + +## πŸ‘· Prerequisites + +1. Have a Github account properly setup with your local Git + > You should use ssh authentication between your local git and + Github. **[How to set it up](https://help.github.com/articles/connecting-to-github-with-ssh)** +2. **Fork** the repository + + ![fork button](img/fork.png) + +3. **Clone** your fork locally + ```shell + cd /path/to/workspace + git clone git@github.com:YOUR_USERNAME/secure-web-dev-workshop1.git + ``` +4. Run the `index.js` file with node + ```shell + node index.js + ``` +5. Check the result, it should display `πŸš€ It Works!` + +## πŸ—’ What to do + +There are multiple `TODO` instructions inside the `index.js` file. You have to do the task associated with each one +of them. + +Commit your changes after resolving each `TODO` + +```shell +git commit -m "feat(feat-name): Explain commit" +``` + +## πŸ“¦ Data structure + +We will work on a public dataset (OpenData) given by French gov and the city of Paris, named "[Lieux de tournage Γ  Paris](https://opendata.paris.fr/explore/dataset/lieux-de-tournage-a-paris/information)" + +This dataset represents a list of locations for outdoor scene shooting in Paris since 2016. It does not include data of +the current year. + +Here is an element extracted from the dataset, to show the data structure: + +```json +{ + "datasetid": "lieux-de-tournage-a-paris", + "recordid": "737d97372281f1d51fe3294aae21179ca00e2e05", + "fields": { + "coord_y": 48.83566, + "type_tournage": "Long m\u00e9trage", + "nom_producteur": "MANDARIN PRODUCTION", + "date_fin": "2020-08-21", + "geo_point_2d": [ + 48.83566000015182, + 2.348314535961912 + ], + "nom_tournage": "TOUT S'EST BIEN PASSE", + "ardt_lieu": "75013", + "geo_shape": { + "coordinates": [ + 2.348314535961912, + 48.83566000015182 + ], + "type": "Point" + }, + "id_lieu": "2020-404", + "nom_realisateur": "Francois OZON", + "adresse_lieu": "rue pascal, 75013 paris", + "date_debut": "2020-08-20", + "annee_tournage": "2020", + "coord_x": 2.34831454 + }, + "geometry": { + "type": "Point", + "coordinates": [ + 2.348314535961912, + 48.83566000015182 + ] + }, + "record_timestamp": "2022-02-21T12:01:17.756+01:00" +} +``` diff --git a/README.md b/README.md index 0e30b9c..9463fa4 100644 --- a/README.md +++ b/README.md @@ -1,85 +1,13 @@ -# Manipulate Data with Javascript and NodeJS - -## 🌟 Goal - -> Learning Javascript basics by manipulating arrays, objects, functions etc... - -## πŸ‘· Prerequisites - -1. Have a Github account properly setup with your local Git - > You should use ssh authentication between your local git and - Github. **[How to set it up](https://help.github.com/articles/connecting-to-github-with-ssh)** -2. **Fork** the repository - - ![fork button](img/fork.png) - -3. **Clone** your fork locally - ```shell - cd /path/to/workspace - git clone git@github.com:YOUR_USERNAME/secure-web-dev-workshop1.git - ``` -4. Run the `index.js` file with node - ```shell - node index.js - ``` -5. Check the result, it should display `πŸš€ It Works!` - -## πŸ—’ What to do - -There are multiple `TODO` instructions inside the `index.js` file. You have to do the task associated with each one -of them. - -Commit your changes after resolving each `TODO` - -```shell -git commit -m "feat(feat-name): Explain commit" -``` - -## πŸ“¦ Data structure - -We will work on a public dataset (OpenData) given by French gov and the city of Paris, named "[Lieux de tournage Γ  Paris](https://opendata.paris.fr/explore/dataset/lieux-de-tournage-a-paris/information)" - -This dataset represents a list of locations for outdoor scene shooting in Paris since 2016. It does not include data of -the current year. - -Here is an element extracted from the dataset, to show the data structure: - -```json -{ - "datasetid": "lieux-de-tournage-a-paris", - "recordid": "737d97372281f1d51fe3294aae21179ca00e2e05", - "fields": { - "coord_y": 48.83566, - "type_tournage": "Long m\u00e9trage", - "nom_producteur": "MANDARIN PRODUCTION", - "date_fin": "2020-08-21", - "geo_point_2d": [ - 48.83566000015182, - 2.348314535961912 - ], - "nom_tournage": "TOUT S'EST BIEN PASSE", - "ardt_lieu": "75013", - "geo_shape": { - "coordinates": [ - 2.348314535961912, - 48.83566000015182 - ], - "type": "Point" - }, - "id_lieu": "2020-404", - "nom_realisateur": "Francois OZON", - "adresse_lieu": "rue pascal, 75013 paris", - "date_debut": "2020-08-20", - "annee_tournage": "2020", - "coord_x": 2.34831454 - }, - "geometry": { - "type": "Point", - "coordinates": [ - 2.348314535961912, - 48.83566000015182 - ] - }, - "record_timestamp": "2022-02-21T12:01:17.756+01:00" -} -``` +# Workshop 1 + +First Secure Web Development workshop. + +### Description + +The goal was to play around with a `.json` file and its contents. + +### How to execute + +The code is all inside `index.js` and can simply be executed by uncommenting the wanted function and running the code. + +### Contents diff --git a/index.js b/index.js index 9b48491..21d838e 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ // https://opendata.paris.fr/explore/dataset/lieux-de-tournage-a-paris/information const filmingLocations = require('./lieux-de-tournage-a-paris.json') -console.log('πŸš€ It Works!'); +//console.log('πŸš€ It Works!'); /** * πŸ’… Try to produce the most readable code, use meaningful variable names @@ -15,25 +15,27 @@ console.log('πŸš€ It Works!'); // πŸ“ 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. // 1. Implement the function // 2. Log the first and last item in array -function sortFilmingLocationsByStartDate () { - return '' +function sortFilmingLocationsByStartDate(array) { + return array.sort(function(a, b){return new Date(b.fields.date_debut) - new Date(a.fields.date_debut);}); } -console.log(``) +var sortedFilmingLocations = sortFilmingLocationsByStartDate(filmingLocations); + +//console.log(sortedFilmingLocations[0], sortedFilmingLocations[sortedFilmingLocations.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 '' + return filmingLocations.filter((check)=>check.fields.annee_tournage==2020).length; } -console.log() +//console.log(getFilmingLocationsNumber2020()) // πŸ“ TODO: Number of filming locations per year // 1. Implement the function, the expected result is an object with years as @@ -44,9 +46,14 @@ console.log() // } // 2. Log the result function getFilmingLocationsNumberPerYear () { - return {} + var count = {} + filmingLocations.forEach(function(a) { + var key = a.fields.annee_tournage + count[key] = (count[key] || 0) + 1 + }) + return count } -console.log() +//console.log(getFilmingLocationsNumberPerYear()) // πŸ“ TODO: Number of filming locations by district (arrondissement) // 1. Implement the function, the expected result is an object with @@ -57,32 +64,61 @@ console.log() // } // 2. Log the result function getFilmingLocationsNumberPerDistrict () { - return {} + var count = {} + filmingLocations.forEach(function(a) { + var key = a.fields.ardt_lieu + count[key] = (count[key] || 0) + 1 + }) + return count } -console.log() +//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 [] + var count = {} + filmingLocations.forEach(function(a) { + var key = a.fields.nom_tournage + count[key] = (count[key] || 0) + 1 + }) + + let sortable = []; + for (var a in count) { + sortable.push([a, count[a]]); + } + + sortable.sort(function(a, b) { + return b[1] - a[1]; + }); + + return sortable } -console.log() +//console.log(getFilmLocationsByFilm()) // πŸ“ TODO: Number of different films // 1. Implement the function // 2. Log the result function getNumberOfFilms() { - return '' + return getFilmLocationsByFilm().length } +//console.log(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 [] + let locations = [] + for(var i of filmingLocations){ + if(i.fields.nom_tournage === "LRDM - Patriot season 2"){ + locations.push(i.fields.adresse_lieu) + } + } + return locations + } +//console.log(getArseneFilmingLocations()) // πŸ“ TODO: Tous les arrondissement des lieux de tournage de nos films favoris // (favoriteFilms) @@ -91,7 +127,18 @@ function getArseneFilmingLocations () { // const films = { 'LRDM - Patriot season 2': ['75013'] } // 2. Log the result function getFavoriteFilmsLocations (favoriteFilmsNames) { - return [] + const films = {} + for (let i of filmingLocations) { + let key = i.fields; + if (favoriteFilmsNames.includes(key.nom_tournage)) + { + if (!films[key.nom_tournage]){ + films[key.nom_tournage]=[]; + } + films[key.nom_tournage].push(key.ardt_lieu); + } + } + return [films]; } const favoriteFilms = [ @@ -100,6 +147,8 @@ const favoriteFilms = 'Emily in Paris', ] +//console.log(getFavoriteFilmsLocations(favoriteFilms)) + // πŸ“ TODO: All filming locations for each film // e.g. : // const films = { @@ -107,24 +156,53 @@ const favoriteFilms = // 'Une jeune fille qui va bien': [{...}] // } function getFilmingLocationsPerFilm () { - return { } + let count = {} + for (var i=0; i `${(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 longestDuration(){ + return filmingLocations.sort(function(a, b){return (new Date(b.fields.date_fin) - new Date(b.fields.date_debut)) - (new Date(a.fields.date_fin) - new Date(a.fields.date_debut))}); +} +//console.log(longestDuration()[0], duration(new Date(longestDuration()[0].fields.date_fin).getTime() - new Date(longestDuration()[0].fields.date_debut).getTime())) // πŸ“ TODO: Compute the average filming duration // 1. Implement the function // 2. Log the result + +function averageDuration() { + let total = 0; + for (var i of filmingLocations) { + let key = i.fields + let d1 = new Date(key.date_fin); + let d2 = new Date(key.date_debut); + let d3 = d1.getTime()-d2.getTime() + total += d3; + } + return duration(total/filmingLocations.length); +} + +console.log(averageDuration()) \ No newline at end of file