Last active
May 6, 2025 01:23
-
-
Save ivnvaldz7/b94bb544a5fd1725bf000dcad1f9f867 to your computer and use it in GitHub Desktop.
Ejercicio mejorado
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { | |
| showPeliculas, | |
| sortPeliculas, | |
| searchPeliculas, | |
| tagPeliculas, | |
| } = require("./pelis.js"); | |
| function main() { | |
| console.log("Bienvenido a la aplicación de películas"); | |
| console.log("Aquí puedes ver, buscar y ordenar películas."); | |
| const key = process.argv[2] ? process.argv[2].toLowerCase() : undefined; | |
| const value = process.argv[3] ? process.argv[3].toLowerCase() : undefined; | |
| switch (key) { | |
| case "--help": | |
| console.log("Uso: node index.js [opción] [valor]"); | |
| console.log("Opciones:"); | |
| console.log( | |
| "--sort [parametro]: Ordenar películas por el parámetro especificado (año, título, etc.)." | |
| ); | |
| console.log("--search [titulo]: Buscar una película por su título."); | |
| console.log( | |
| "--tag [etiqueta]: Filtrar películas por una etiqueta específica." | |
| ); | |
| break; | |
| case undefined: | |
| showPeliculas(); | |
| break; | |
| case "--sort": | |
| sortPeliculas(value); | |
| break; | |
| case "--search": | |
| if (!value) { | |
| console.log("Error: Se necesita un valor para buscar."); | |
| } else { | |
| searchPeliculas(value); | |
| } | |
| break; | |
| case "--tag": | |
| if (!value) { | |
| console.log("Error: Se necesita un valor para filtrar por etiqueta."); | |
| } else { | |
| tagPeliculas(value); | |
| } | |
| break; | |
| default: | |
| console.log( | |
| "Opción no válida. Usa --help para ver las opciones disponibles." | |
| ); | |
| } | |
| console.log("Gracias por usar la aplicación de películas."); | |
| console.log("Hasta luego!"); | |
| } | |
| main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require("fs"); | |
| const data = fs.readFileSync(__dirname + "/pelis.json"); | |
| const peliculas = JSON.parse(data); | |
| function showPeliculas() { | |
| console.log("Peliculas disponibles:"); | |
| peliculas.forEach((pelicula) => { | |
| console.log(`- ${pelicula.title} (${pelicula.anio})`); | |
| }); | |
| } | |
| function sortPeliculas(param) { | |
| peliculas.sort((a, b) => { | |
| if (a[param] < b[param]) return -1; | |
| if (a[param] > b[param]) return 1; | |
| return 0; | |
| }); | |
| console.log(`Peliculas ordenadas por ${param}:`); | |
| peliculas.forEach((pelicula) => { | |
| console.log(`- ${pelicula.title} (${pelicula.anio})`); | |
| }); | |
| } | |
| function searchPeliculas(title) { | |
| const resultados = peliculas.filter(p => | |
| p.title.toLowerCase().includes(title.toLowerCase()) | |
| ); | |
| if (resultados.length > 0) { | |
| console.log(`Resultados de la búsqueda para "${title}":`); | |
| resultados.forEach(peli => { | |
| console.log(`- ${peli.title} (${peli.anio})`); | |
| }); | |
| } else { | |
| console.log(`No se encontraron resultados para "${title}".`); | |
| } | |
| return resultados; | |
| } | |
| // Unificar las funciones relacionadas con tags para evitar redundancia. | |
| function tagPeliculas(tag) { | |
| const resultados = peliculas.filter( | |
| (pelicula) => pelicula.tags && pelicula.tags.includes(tag) | |
| ); | |
| if (resultados.length > 0) { | |
| console.log(`Peliculas con la etiqueta "${tag}":`); | |
| resultados.forEach((pelicula) => { | |
| console.log(`- ${pelicula.title} (${pelicula.anio})`); | |
| }); | |
| } else { | |
| console.log(`No se encontraron peliculas con la etiqueta "${tag}".`); | |
| } | |
| return resultados; | |
| } | |
| module.exports = { | |
| showPeliculas, | |
| sortPeliculas, | |
| searchPeliculas, | |
| tagPeliculas, | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { | |
| "title": "Avengers", | |
| "rating": 4, | |
| "tags": ["accion", "ciencia ficcion"], | |
| "anio": 2012, | |
| "director": "Joss Whedon" | |
| }, | |
| { | |
| "title": "Ghost", | |
| "rating": 3, | |
| "tags": ["romance"], | |
| "anio": 1990, | |
| "director": "Jerry Zucker" | |
| }, | |
| { | |
| "title": "Anabelle", | |
| "rating": 4, | |
| "tags": ["terror", "paranormal"], | |
| "anio": 2014, | |
| "director": "John R. Leonetti" | |
| }, | |
| { | |
| "title": "Interestelar", | |
| "rating": 5, | |
| "tags": ["drama", "ciencia ficcion", "documental"], | |
| "anio": 2014, | |
| "director": "Christopher Nolan" | |
| }, | |
| { | |
| "title": "Luca", | |
| "rating": 3, | |
| "tags": ["niños", "animacion", "romance"], | |
| "anio": 2021, | |
| "director": "Enrico Casarosa" | |
| } | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment