Skip to content

Commit 08f7a95

Browse files
committed
#20 - javascript
1 parent 20f79cd commit 08f7a95

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
async function peticionHTTP() {
2+
try {
3+
const respuesta = await fetch('https://edalmava.co')
4+
if (respuesta.ok) {
5+
const datos = await respuesta.text()
6+
console.log(datos)
7+
} else {
8+
console.log("Respuesta de red OK pero respuesta HTTP no OK")
9+
}
10+
} catch(error) {
11+
console.log("Hubo un problema con la petición Fetch:" + error.message)
12+
}
13+
}
14+
15+
async function buscarPokemon(name) {
16+
try {
17+
const respuesta = await fetch('https://pokeapi.co/api/v2/pokemon/' + name)
18+
if (respuesta.ok) {
19+
const datos = await respuesta.json()
20+
//console.log(datos)
21+
const nombre = datos.name
22+
const id = datos.id
23+
const peso = datos.weight
24+
const altura = datos.height
25+
const tipos = datos.types.map(e => e.type.name).join(',')
26+
27+
console.log('Información del Pokémon: ')
28+
console.log('Nombre: ', nombre)
29+
console.log('ID: ', id)
30+
console.log('Peso: ', peso)
31+
console.log('Altura: ', altura)
32+
console.log('Tipo(s): ', tipos)
33+
} else {
34+
console.log("Respuesta de red OK pero respuesta HTTP no OK")
35+
}
36+
} catch(error) {
37+
console.log("Hubo un problema con la petición Fetch:" + error.message)
38+
}
39+
}
40+
41+
async function inicio() {
42+
console.log('*****Petición HTTP al sitio https://edalmava.co*****')
43+
await peticionHTTP()
44+
console.log('****************************************************')
45+
console.log('****************************************************')
46+
console.log('*****RETO EXTRA*****')
47+
const readline = require('node:readline');
48+
const rl = readline.createInterface({
49+
input: process.stdin,
50+
output: process.stdout,
51+
});
52+
rl.question(`¿Nombre o número del Pokémon a buscar? `, name => {
53+
buscarPokemon(name)
54+
rl.close();
55+
});
56+
}
57+
58+
inicio()

0 commit comments

Comments
 (0)