-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (52 loc) · 1.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* eslint-disable no-console */
async function fetchWebsiteContent(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.text();
console.log('Request was successful!');
console.log('Website content:\n', data);
} catch (error) {
console.error('Failed to fetch the website content. Error:', error.message);
}
}
fetchWebsiteContent('https://google.com');
async function fetchPokemonData(pokemon) {
try {
const response = await
fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon.toLowerCase()}`);
if (!response.ok) {
throw new Error(`Pokémon not found! Status: ${response.status}`);
}
const pokemonData = await response.json();
console.log(`Name: ${pokemonData.name}`);
console.log(`ID: ${pokemonData.id}`);
console.log(`Weight: ${pokemonData.weight}`);
console.log(`Height: ${pokemonData.height}`);
console.log('Types:', pokemonData.types
.map((typeInfo) => typeInfo.type.name)
.join(', '));
// Obtener la cadena de evoluciones
const speciesResponse = await fetch(pokemonData.species.url);
const speciesData = await speciesResponse.json();
const evolutionResponse = await fetch(speciesData.evolution_chain.url);
const evolutionData = await evolutionResponse.json();
const evolutionChain = [];
let currentStage = evolutionData.chain;
while (currentStage) {
evolutionChain.push(currentStage.species.name);
const [firstEvolution] = currentStage.evolves_to;
currentStage = firstEvolution;
}
console.log('Evolution Chain:', evolutionChain.join(' -> '));
// Obtener los juegos en los que aparece el Pokémon
console.log('Games:', pokemonData.game_indices
.map((game) => game.version.name)
.join(', '));
} catch (error) {
console.error('Failed to fetch Pokémon data. Error:', error.message);
}
}
fetchPokemonData('pikachu');