Skip to content

Instantly share code, notes, and snippets.

@ivnvaldz7
Last active October 12, 2024 22:23
Show Gist options
  • Select an option

  • Save ivnvaldz7/4b6df3835343078a6b24ddd2c83fa93f to your computer and use it in GitHub Desktop.

Select an option

Save ivnvaldz7/4b6df3835343078a6b24ddd2c83fa93f to your computer and use it in GitHub Desktop.
Primer desafio fuerte de la carrera.
const { mostrarPelis, sort, search, tag } = require("./pelis");
function main() {
const key = process.argv[2] ? process.argv[2].toLowerCase() : undefined
const value = process.argv[3] ? process.argv[3].toLowerCase() : undefined
console.log(value);
if (!key) {
mostrarPelis();
}
if (key === "--sort") {
sort(value);
}
if (key === "--search") {
if (!value) {
console.log("Error: Se necesita un valor para buscar.");
} else {
const resultados = search(value);
console.log(resultados);
}
}
if (key === "--tag") {
const resultados = tag(value);
console.log(resultados);
}
}
main();
const fs = require("fs");
const data = fs.readFileSync(__dirname + "/pelis.json");
const peliculas = JSON.parse(data);
function mostrarPelis() {
console.table(peliculas);
}
function sort(value) {
if (!value) {
sort("title");
return;
}
if (value === "title" || value === "rating" || value === "tags") {
const peliculasOrdenadas = peliculas.sort((a, b) => {
if (Array.isArray(a[value]) && Array.isArray(b[value])) {
return a[value].length - b[value].length; // Ordena por longitud del array
} else if (typeof a[value] === "string" && typeof b[value] === "string") {
return a[value].localeCompare(b[value]);
} else {
return a[value] - b[value];
}
});
console.table(peliculasOrdenadas);
} else {
console.log("Propiedad no válida");
}
}
function search(value) {
const palabraFiltrada = peliculas.filter((pelicula) => {
return (
pelicula.title === value ||
pelicula.rating === value ||
(Array.isArray(pelicula.tags) && pelicula.tags.includes(value))
);
});
console.log(palabraFiltrada);
}
function tag(value) {
const filtrarTags = peliculas.filter((pelicula) => {
return Array.isArray(pelicula.tags) && pelicula.tags.includes(value);
});
console.log(filtrarTags);
}
module.exports = {
mostrarPelis,
sort,
search,
tag,
};
[
{
"title": "Avengers",
"rating": 4,
"tags": ["acción", "comedia", "heroica"]
},
{
"title": "Ghost",
"rating": 3,
"tags": ["romance"]
},
{
"title": "Anabelle",
"rating": 4,
"tags": ["terror", "paranormal"]
},
{
"title": "Interestelar",
"rating": 5,
"tags": ["drama", "ciencia", "documental"]
},
{
"title": "Luca",
"rating": 3,
"tags": ["niños", "animacion"]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment