-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (65 loc) · 1.86 KB
/
script.js
File metadata and controls
75 lines (65 loc) · 1.86 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
let fs = require('fs');
let fetch = require('node-fetch');
class PokeInfo
{
constructor(newInputFile = './input.txt')
{
this.inputFile = newInputFile;
}
getInfo()
{
//fs.open(this.inputFile, 'w', (err) =>
//{
// if (err) throw err;
//});
fs.readFile(this.inputFile, (err, data) =>
{
if (err)
{
throw err;
}
this.fetchData(data.toString());
})
}
fetchData(inData)
{
//console.log(data);
const pokemonArray = inData.split('\n');
pokemonArray.forEach((element) =>
{
const pokemonName = element.toLowerCase();
fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonName}`)
.then(response => response.json())
.then(data =>
{
this.displayPokemonInfo(data);
})
.catch(() =>
{
console.log("Error");
});
});
}
displayPokemonInfo(pokemonObject)
{
//console.log(pokemonObject);
let displayString = `${pokemonObject.name}: `
//console.log(pokemonObject.types.type.name.join(','));
for (let i = 0; i < pokemonObject.types.length; ++i)
{
displayString += pokemonObject.types[i].type.name + ", ";
// displayString += pokemonObject.types[i].type.name;
}
displayString = displayString.substring(0, displayString.length - 2);
console.log(displayString);
}
}
let pokeInfo = new PokeInfo();
pokeInfo.getInfo();
// -----
//let pokemon = fetch('https://pokeapi.co/api/v2/pokemon/charmander')
// .then(response => response.json())
// .then(data => {
// console.log("Name: " + data.name);
// console.log("Type: " + data[0].type.name);
// });