-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (42 loc) · 1.21 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
#!/usr/bin/env node
// const yargs = require("yargs");
// const { argv } = yargs(process.argv);
const inquirer = require("inquirer");
const fetch = require("node-fetch");
const printFiveMoves = async (pokemonName) => {
try {
const response = await fetch(
`https://pokeapi.co/api/v2/pokemon/${pokemonName}`
);
const pokemon = await response.json();
const moves = pokemon.moves.map(({ move }) => move.name);
console.log(moves.slice(0, 5));
} catch (error) {
console.error("Error:", error.message);
}
};
const prompt = inquirer.createPromptModule();
prompt([
{
type: "input",
name: "pokemon",
message: "Enter a pokemon to see it's first 5 moves",
},
]).then((answers) => {
const pokemon = answers.pokemon;
printFiveMoves(pokemon);
});
//Another way to fetch data
// let url = `https://pokeapi.co/api/v2/pokemon/charmander`;
// fetch(url)
// .then((response) => response.json())
// .then((data) => {
// // Process the retrieved data
// const moves = data.moves.map(({ move }) => move.name);
// console.log(moves.slice(0, 5));
// // console.log(data);
// })
// .catch((error) => {
// // Handle any errors
// console.error("Error:", error);
// });