|
| 1 | +/* |
| 2 | + * EJERCICIO: |
| 3 | + * Utilizando un mecanismo de peticiones HTTP de tu lenguaje, realiza |
| 4 | + * una petición a la web que tú quieras, verifica que dicha petición |
| 5 | + * fue exitosa y muestra por consola el contenido de la web. |
| 6 | + * |
| 7 | + * DIFICULTAD EXTRA (opcional): |
| 8 | + * Utilizando la PokéAPI (https://pokeapi.co), crea un programa por |
| 9 | + * terminal al que le puedas solicitar información de un Pokémon concreto |
| 10 | + * utilizando su nombre o número. |
| 11 | + * - Muestra el nombre, id, peso, altura y tipo(s) del Pokémon |
| 12 | + * - Muestra el nombre de su cadena de evoluciones |
| 13 | + * - Muestra los juegos en los que aparece |
| 14 | + * - Controla posibles errores |
| 15 | + */ |
| 16 | + |
| 17 | +fetch("https://pokeapi.co/api/v2/pokemon/pikachu") |
| 18 | + .then((resp) => { |
| 19 | + return resp.json(); |
| 20 | + }) |
| 21 | + .then((data) => { |
| 22 | + console.log(data); |
| 23 | + }) |
| 24 | + .catch((error) => { |
| 25 | + console.error("Error fetching data:", error); |
| 26 | + }); |
| 27 | + |
| 28 | +const readline = require("readline"); |
| 29 | +const rl = readline.createInterface(process.stdin, process.stdout); |
| 30 | + |
| 31 | +rl.question( |
| 32 | + "Dame el nombre de un Pokemon y te diré lo que sé de él -> ", |
| 33 | + (resp) => { |
| 34 | + const pokemon = resp; |
| 35 | + |
| 36 | + fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon}`) |
| 37 | + .then((resp) => { |
| 38 | + if (resp.status === 404) { |
| 39 | + console.log("No se ha encontrado el nombre del pokemon"); |
| 40 | + return; |
| 41 | + } |
| 42 | + return resp.json(); |
| 43 | + }) |
| 44 | + .then( async (data) => { |
| 45 | + if (data != undefined) { |
| 46 | + console.log( |
| 47 | + `Nombre: ${data.name}, id: ${data.id}, peso: ${data.weight}, altura: ${data.height}` |
| 48 | + ); |
| 49 | + console.log("Tipos:"); |
| 50 | + data.types.forEach((type) => { |
| 51 | + console.log("-", type.type.name); |
| 52 | + }); |
| 53 | + console.log('Juegos en los que aparece:'); |
| 54 | + data.game_indices.forEach(game => { |
| 55 | + console.log('-', game.version.name); |
| 56 | + }); |
| 57 | + |
| 58 | + const speciesResponse = await fetch(data.species.url); |
| 59 | + const speciesData = await speciesResponse.json(); |
| 60 | + const evolutionChainUrl = speciesData.evolution_chain.url; |
| 61 | + const evolutionChainResponse = await fetch(evolutionChainUrl); |
| 62 | + const evolutionChainData = await evolutionChainResponse.json(); |
| 63 | + |
| 64 | + console.log('Cadena de evolución:'); |
| 65 | + let evolutionChain = evolutionChainData.chain; |
| 66 | + while (evolutionChain) { |
| 67 | + console.log('-', evolutionChain.species.name); |
| 68 | + evolutionChain = evolutionChain['evolves_to'][0]; |
| 69 | + } |
| 70 | + } |
| 71 | + }) |
| 72 | + .catch((error) => { |
| 73 | + console.error("Error fetching data:", error); |
| 74 | + }); |
| 75 | + } |
| 76 | +); |
0 commit comments