Skip to content

Commit fa9418d

Browse files
committed
#20 - JavaScript
1 parent 0f1fd3c commit fa9418d

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// ** EJERCICIO
2+
3+
let url = 'https://bernatcucarella.com'
4+
5+
fetch(url)
6+
.then(response => response.text())
7+
.then(data => console.log(data))
8+
9+
10+
// ** DIFICULTAD EXTRA ** ----------------------------------------------------------------------------------------------------------------------------------------------------
11+
12+
// const https = require('node:https');
13+
14+
// function obtenerInfoPokemon(nombreONumero) {
15+
// const url = `https://pokeapi.co/api/v2/pokemon/${nombreONumero.toLowerCase()}`;
16+
17+
// https.get(url, (response) => {
18+
// let data = '';
19+
20+
// // Recibir los datos en trozos
21+
// response.on('data', (chunk) => {
22+
// data += chunk;
23+
// });
24+
25+
// // Cuando se termina de recibir la respuesta
26+
// response.on('end', () => {
27+
// if (response.statusCode === 200) {
28+
// try {
29+
// const pokemon = JSON.parse(data);
30+
31+
// // Mostrar información del Pokémon
32+
// console.log(`Nombre: ${pokemon.name}`);
33+
// console.log(`ID: ${pokemon.id}`);
34+
// console.log(`Peso: ${pokemon.weight} kg`);
35+
// console.log(`Altura: ${pokemon.height * 10} cm`); // Altura en decímetros
36+
// console.log(`Tipos: ${pokemon.types.map(type => type.type.name).join(', ')}`);
37+
38+
// // Obtener la cadena de evolución del Pokémon
39+
// obtenerCadenaEvolucion(pokemon.species.url);
40+
// } catch (error) {
41+
// console.error('Error al parsear la respuesta JSON:', error.message);
42+
// }
43+
// } else {
44+
// console.error(`Error al obtener información del Pokémon. Código de estado: ${response.statusCode}`);
45+
// }
46+
// });
47+
// }).on('error', (error) => {
48+
// console.error('Error al hacer la solicitud:', error.message);
49+
// });
50+
// }
51+
52+
// function obtenerCadenaEvolucion(speciesUrl) {
53+
// https.get(speciesUrl, (response) => {
54+
// let data = '';
55+
56+
// // Recibir los datos en trozos
57+
// response.on('data', (chunk) => {
58+
// data += chunk;
59+
// });
60+
61+
// // Cuando se termina de recibir la respuesta
62+
// response.on('end', () => {
63+
// if (response.statusCode === 200) {
64+
// try {
65+
// const species = JSON.parse(data);
66+
// const evolutionChainUrl = species.evolution_chain.url;
67+
68+
// // Obtener la cadena de evolución desde la URL de la cadena
69+
// https.get(evolutionChainUrl, (response) => {
70+
// let data = '';
71+
72+
// // Recibir los datos en trozos
73+
// response.on('data', (chunk) => {
74+
// data += chunk;
75+
// });
76+
77+
// // Cuando se termina de recibir la respuesta
78+
// response.on('end', () => {
79+
// if (response.statusCode === 200) {
80+
// try {
81+
// const evolutionChain = JSON.parse(data);
82+
// const cadenaEvolucion = obtenerNombreCadenaEvolucion(evolutionChain.chain);
83+
// console.log(`Cadena de Evolución: ${cadenaEvolucion}`);
84+
// } catch (error) {
85+
// console.error('Error al parsear la cadena de evolución:', error.message);
86+
// }
87+
// } else {
88+
// console.error(`Error al obtener la cadena de evolución. Código de estado: ${response.statusCode}`);
89+
// }
90+
// });
91+
// }).on('error', (error) => {
92+
// console.error('Error al hacer la solicitud de la cadena de evolución:', error.message);
93+
// });
94+
95+
// } catch (error) {
96+
// console.error('Error al parsear la respuesta JSON:', error.message);
97+
// }
98+
// } else {
99+
// console.error(`Error al obtener información de la especie del Pokémon. Código de estado: ${response.statusCode}`);
100+
// }
101+
// });
102+
// }).on('error', (error) => {
103+
// console.error('Error al hacer la solicitud de la especie del Pokémon:', error.message);
104+
// });
105+
// }
106+
107+
// function obtenerNombreCadenaEvolucion(chain) {
108+
// let cadena = '';
109+
// let currentPokemon = chain.species.name;
110+
111+
// if (chain.evolves_to.length > 0) {
112+
// cadena += currentPokemon + ' -> ';
113+
// let nextChain = chain.evolves_to[0];
114+
// while (nextChain) {
115+
// cadena += nextChain.species.name + ' -> ';
116+
// nextChain = nextChain.evolves_to[0];
117+
// }
118+
// cadena = cadena.slice(0, -4); // Eliminar el último ' -> '
119+
// } else {
120+
// cadena = currentPokemon;
121+
// }
122+
123+
// return cadena;
124+
// }
125+
126+
// // Capturar el nombre o número del Pokémon desde la línea de comandos
127+
// const nombreONumero = process.argv[2];
128+
129+
// if (!nombreONumero) {
130+
// console.error('Por favor, proporciona el nombre o número del Pokémon como argumento.');
131+
// } else {
132+
// obtenerInfoPokemon(nombreONumero);
133+
// }

0 commit comments

Comments
 (0)