From 285bd1280485d9fd84b62bdff24ae8316e04893b Mon Sep 17 00:00:00 2001 From: afminguela Date: Thu, 19 Jun 2025 13:31:18 +0200 Subject: [PATCH 1/3] iteration 5 --- src/movies.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/src/movies.js b/src/movies.js index 1ad02ff68..584ae6236 100644 --- a/src/movies.js +++ b/src/movies.js @@ -1,19 +1,72 @@ + + // Iteration 1: All directors? - Get the array of all directors. + +// STRUCTURE OF THE DATA Objects. movies[i].clave +// title: 'The Shawshank Redemption', +// year: 1994, +// director: 'Frank Darabont', +// duration: '2h 22min', +// genre: ['Crime', 'Drama'], +// score: 9.3 +// + + + // _Bonus_: It seems some of the directors had directed multiple movies so they will pop up multiple times in the array of directors. // How could you "clean" a bit this array and make it unified (without duplicates)? -function getAllDirectors(moviesArray) {} +function getAllDirectors(moviesArray) { + const allDirectors =moviesArray.map(moviesArray => moviesArray.director) + return allDirectors +} + +console.log(getAllDirectors(movies)); // Iteration 2: Steven Spielberg. The best? - How many drama movies did STEVEN SPIELBERG direct? -function howManyMovies(moviesArray) {} +function howManyMovies(moviesArray) { + return moviesArray.filter( + movie => + movie.director === "Steven Spielberg" && + movie.genre.includes("Drama") + ).length; +} +lo // Iteration 3: All scores average - Get the average of all scores with 2 decimals -function scoresAverage(moviesArray) {} +function scoresAverage(moviesArray) { + if (moviesArray.length === 0) return 0; + const total = moviesArray.reduce((sum, movie) => sum + (movie.score || 0), 0); + const media= total / moviesArray.length; + + return parseFloat(media.toFixed(2)) + + +} // Iteration 4: Drama movies - Get the average of Drama Movies -function dramaMoviesScore(moviesArray) {} +function dramaMoviesScore(moviesArray) { + +const dramas= moviesArray.filter(elemento => elemento.genre.includes("Drama")) + +return scoresAverage(dramas) + +} +console.log("iteration 5"); // Iteration 5: Ordering by year - Order by year, ascending (in growing order) -function orderByYear(moviesArray) {} +function orderByYear(moviesArray) { + const arraycopy= [...moviesArray] +const moviesOrdered = arraycopy.sort((a,b) => { + a.year - b.year +}) + +return moviesOrdered +} + + + + +console.log(orderByYear(movies)); // Iteration 6: Alphabetic Order - Order by title and print the first 20 titles function orderAlphabetically(moviesArray) {} From 3d19760d1c2c153fefc52d96f7023009695f5f73 Mon Sep 17 00:00:00 2001 From: afminguela Date: Thu, 19 Jun 2025 16:17:48 +0200 Subject: [PATCH 2/3] done --- src/movies.js | 116 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 81 insertions(+), 35 deletions(-) diff --git a/src/movies.js b/src/movies.js index 584ae6236..82358e14e 100644 --- a/src/movies.js +++ b/src/movies.js @@ -1,5 +1,3 @@ - - // Iteration 1: All directors? - Get the array of all directors. // STRUCTURE OF THE DATA Objects. movies[i].clave @@ -9,70 +7,118 @@ // duration: '2h 22min', // genre: ['Crime', 'Drama'], // score: 9.3 -// - - +// // _Bonus_: It seems some of the directors had directed multiple movies so they will pop up multiple times in the array of directors. // How could you "clean" a bit this array and make it unified (without duplicates)? function getAllDirectors(moviesArray) { - const allDirectors =moviesArray.map(moviesArray => moviesArray.director) - return allDirectors + const allDirectors = moviesArray.map((moviesArray) => moviesArray.director); + return allDirectors; } - + console.log(getAllDirectors(movies)); // Iteration 2: Steven Spielberg. The best? - How many drama movies did STEVEN SPIELBERG direct? function howManyMovies(moviesArray) { - return moviesArray.filter( - movie => - movie.director === "Steven Spielberg" && - movie.genre.includes("Drama") + (movie) => + movie.director === "Steven Spielberg" && movie.genre.includes("Drama") ).length; } -lo +lo; // Iteration 3: All scores average - Get the average of all scores with 2 decimals function scoresAverage(moviesArray) { - if (moviesArray.length === 0) return 0; - const total = moviesArray.reduce((sum, movie) => sum + (movie.score || 0), 0); - const media= total / moviesArray.length; - - return parseFloat(media.toFixed(2)) - + if (moviesArray.length === 0) return 0; + const total = moviesArray.reduce((sum, movie) => sum + (movie.score || 0), 0); + const media = total / moviesArray.length; + return parseFloat(media.toFixed(2)); } +console.log(movies); // Iteration 4: Drama movies - Get the average of Drama Movies function dramaMoviesScore(moviesArray) { + const dramas = moviesArray.filter((elemento) => + elemento.genre.includes("Drama") + ); -const dramas= moviesArray.filter(elemento => elemento.genre.includes("Drama")) - -return scoresAverage(dramas) - + return scoresAverage(dramas); } console.log("iteration 5"); // Iteration 5: Ordering by year - Order by year, ascending (in growing order) function orderByYear(moviesArray) { - const arraycopy= [...moviesArray] -const moviesOrdered = arraycopy.sort((a,b) => { - a.year - b.year -}) - -return moviesOrdered + const arraycopy = [...moviesArray]; + arraycopy.sort((a, b) => { + if (a.year !== b.year) { + return a.year - b.year; + } else { + return a.title.localeCompare(b.title); + } + }); + return arraycopy; } - - - console.log(orderByYear(movies)); // Iteration 6: Alphabetic Order - Order by title and print the first 20 titles -function orderAlphabetically(moviesArray) {} +function orderAlphabetically(moviesArray) { + const arraycopy = [...moviesArray]; + const arrayOrdenao = arraycopy.sort((a, b) => { + return a.title.localeCompare(b.title); + }); + const arrayTitulos = arrayOrdenao.map((elemento) => elemento.title); + + return arrayTitulos.splice(0, 20); +} // BONUS - Iteration 7: Time Format - Turn duration of the movies from hours to minutes -function turnHoursToMinutes(moviesArray) {} +function turnHoursToMinutes(moviesArray) { + const arraycopy = [...moviesArray] + return arraycopy.map((elemento) => { + let duration = elemento.duration; + let hours = 0; + let minutes = 0; + + if (typeof duration === "string") { + const hoursMatch = duration.match(/(\d+)h/); + const minutesMatch = duration.match(/(\d+)min/); + + if (hoursMatch) hours = parseInt(hoursMatch[1]); + if (minutesMatch) minutes = parseInt(minutesMatch[1]); + } + + return { + + duration: hours * 60 + minutes, + }; + }); +} // BONUS - Iteration 8: Best yearly score average - Best yearly score average -function bestYearAvg(moviesArray) {} +function bestYearAvg(moviesArray) { + + if(moviesArray.length === 0 ) return null; + const pelisAnual = {}; // paso 1 mete las pelis en un objeto por años + moviesArray.forEach(peli => { // recorre el array mertiendo con push cada peli en su año correspindiente + if (!pelisAnual[peli.year]){ //si no hay año lo crea + pelisAnual[peli.year] =[]; + } + pelisAnual[peli.year].push(peli) //pushea la peli + + }); + // paso 2: calcula la media año. para cada año en el objeto calcula la media. + let mejorMedia=null; + let mejorAño=0; + + for(const year in pelisAnual){ + const media= scoresAverage(pelisAnual[year]); + + if ((media>mejorMedia) || ((media === mejorMedia) && (year < mejorAño))){ + mejorAño =year; + mejorMedia = media; + } + } + return `The best year was ${mejorAño} with an average score of ${mejorMedia}`; + +} From 5079e6bcacbe36cd8885b8c61b2448623f3d3172 Mon Sep 17 00:00:00 2001 From: afminguela Date: Fri, 27 Jun 2025 11:51:40 +0200 Subject: [PATCH 3/3] correcciones y notas --- src/movies.js | 59 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/src/movies.js b/src/movies.js index 82358e14e..7f7cdb1dc 100644 --- a/src/movies.js +++ b/src/movies.js @@ -29,7 +29,7 @@ lo; // Iteration 3: All scores average - Get the average of all scores with 2 decimals function scoresAverage(moviesArray) { if (moviesArray.length === 0) return 0; - const total = moviesArray.reduce((sum, movie) => sum + (movie.score || 0), 0); + const total = moviesArray.reduce((sum, movie) => sum + (movie.score || 0), 0); // si el valor no existe pilla cero const media = total / moviesArray.length; return parseFloat(media.toFixed(2)); @@ -58,6 +58,12 @@ function orderByYear(moviesArray) { }); return arraycopy; } +// super factorizada con ternarios +function orderByYear(moviesArray) { + return [...moviesArray].sort((a, b) => + a.year !== b.year ? a.year - b.year : a.title.localeCompare(b.title) + ); +} console.log(orderByYear(movies)); @@ -72,9 +78,16 @@ function orderAlphabetically(moviesArray) { return arrayTitulos.splice(0, 20); } +// return moviesArray +// .map((e)=>(e.title)) +// .sort((a,b) => a.localeCompare(b)) +// .slice(0,20) + // BONUS - Iteration 7: Time Format - Turn duration of the movies from hours to minutes + + function turnHoursToMinutes(moviesArray) { - const arraycopy = [...moviesArray] + const arraycopy = [...moviesArray]; return arraycopy.map((elemento) => { let duration = elemento.duration; let hours = 0; @@ -89,7 +102,6 @@ function turnHoursToMinutes(moviesArray) { } return { - duration: hours * 60 + minutes, }; }); @@ -97,28 +109,27 @@ function turnHoursToMinutes(moviesArray) { // BONUS - Iteration 8: Best yearly score average - Best yearly score average function bestYearAvg(moviesArray) { - - if(moviesArray.length === 0 ) return null; + if (moviesArray.length === 0) return null; const pelisAnual = {}; // paso 1 mete las pelis en un objeto por años - moviesArray.forEach(peli => { // recorre el array mertiendo con push cada peli en su año correspindiente - if (!pelisAnual[peli.year]){ //si no hay año lo crea - pelisAnual[peli.year] =[]; + moviesArray.forEach((peli) => { + // recorre el array mertiendo con push cada peli en su año correspindiente + if (!pelisAnual[peli.year]) { + //si no hay año lo crea + pelisAnual[peli.year] = []; } - pelisAnual[peli.year].push(peli) //pushea la peli - - }); - // paso 2: calcula la media año. para cada año en el objeto calcula la media. - let mejorMedia=null; - let mejorAño=0; - - for(const year in pelisAnual){ - const media= scoresAverage(pelisAnual[year]); - - if ((media>mejorMedia) || ((media === mejorMedia) && (year < mejorAño))){ - mejorAño =year; - mejorMedia = media; - } - } - return `The best year was ${mejorAño} with an average score of ${mejorMedia}`; + pelisAnual[peli.year].push(peli); //pushea la peli + }); + // paso 2: calcula la media año. para cada año en el objeto calcula la media. + let mejorMedia = null; + let mejorAño = 0; + for (const year in pelisAnual) { + const media = scoresAverage(pelisAnual[year]); + + if (media > mejorMedia || (media === mejorMedia && year < mejorAño)) { + mejorAño = year; + mejorMedia = media; + } + } + return `The best year was ${mejorAño} with an average score of ${mejorMedia}`; }