-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpokemon.js
More file actions
84 lines (65 loc) · 1.9 KB
/
pokemon.js
File metadata and controls
84 lines (65 loc) · 1.9 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
76
77
78
79
80
81
82
83
84
'use strict'
function fetchReq(url){
return fetch(url)
.then(res => res.json())
.catch(err => console.log("Something went wrong"));
}
//array of Pokemon names to randomly populate requests
const pokemon = [
"ditto",
"pikachu",
"gastly",
"eevee",
"psyduck",
"squirtle",
"bulbasaur",
"charizard",
"magikarp",
"mew",
"mewtwo",
"articuno",
"blastoise",
"caterpie",
"metapod",
"pidgeot",
"rattata",
"arbok",
"sandslash",
"nidorina",
"clefairy",
"jigglypuff",
"oddish",
"vileplume"
];
// get two random pokemon
let randomPoke1 = pokemon[Math.floor(Math.random() * pokemon.length)];
let randomPoke2 = pokemon[Math.floor(Math.random() * pokemon.length)];
Promise.all([
fetchReq('https://pokeapi.co/api/v2/pokemon/' + randomPoke1),
fetchReq('https://pokeapi.co/api/v2/pokemon/' + randomPoke2)
]).then(data=>{
getPokemonOne(data["0"].name, data["0"].sprites.front_default, data["0"].stats["4"].base_stat)
getPokemonTwo(data["1"].name, data["1"].sprites.front_default, data["1"].stats["4"].base_stat)
})
//functions to get data to DOM
function getPokemonOne(name,img, attack){
pokemonOne.innerHTML = "<h2>" + name.charAt(0).toUpperCase() + name.slice(1) + "</h2>";
imgOne.innerHTML = `<img src= ${img}>`
attackStat1.innerHTML = `Attack power: ${attack}`
pokeBattle.push(attack);
}
function getPokemonTwo(name,img, attack){
pokemonTwo.innerHTML = "<h2>" + name.charAt(0).toUpperCase() + name.slice(1) + "</h2>";
imgTwo.innerHTML = `<img src= ${img}>`
attackStat2.innerHTML = `Attack power: ${attack}`
pokeBattle.push(attack);
}
let pokeBattle = [];
// battle pokemon
function battle(){
if(pokeBattle[0] > pokeBattle[1]) {
document.getElementById("battle").innerHTML = pokemonOne.innerHTML + " has won!"
} else {
document.getElementById("battle").innerHTML = pokemonTwo.innerHTML + " has won!"
}
}