-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
44 lines (41 loc) · 2.11 KB
/
script.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
const search_term=document.getElementById('search_q')
const search_btn=document.getElementById('search-btn')
// console.log(search_term)
// api http://pokeapi.co/docs/v2#pokemon
const getPokemonData = async term => {
// console.log(${term})
console.log(term);
term=term.toLowerCase();
document.getElementById('show_error').classList.remove('show')
document.getElementById('show_error').classList.add('hidden')
const url= `https://pokeapi.co/api/v2/pokemon/${term}`
const response= await fetch(url)
if(response.status==404 || response.statusText=='Not found')
{
document.getElementById('show_error').classList.add('show')
document.getElementById('show_error').classList.remove('hidden')
return
}
const pokemon =await response.json()
debugger
//update ui with data
document.getElementById('update_img').setAttribute('src',pokemon.sprites.other.dream_world.front_default)
document.getElementById('update_name').innerHTML = pokemon.name
document.getElementById('update_candy_title').innerHTML = `${pokemon.name} Candy`
document.getElementById('update_hp').innerHTML = `HP ${Math.floor((Math.random() * pokemon.stats[0].base_stat) + 1)}/${pokemon.stats[0].base_stat}`
document.getElementById('update_cp').innerHTML = `XP ${pokemon.base_experience}`
document.getElementById('update_type').innerHTML = `${pokemon.types[0].type.name} / ${pokemon.types[1].type.name}`
document.getElementById('update_weight').innerHTML = `${pokemon.weight}kg`
document.getElementById('update_height').innerHTML = `0.${pokemon.height}m`
document.getElementById('update_stardust').innerHTML = Math.floor((Math.random() * 10000) + 1)
document.getElementById('update_candy').innerHTML = Math.floor((Math.random() * 200) + 1)
document.getElementById('update_link').setAttribute('href',`https://www.pokemon.com/us/pokedex/${term}`)
}
search_btn.addEventListener('click', () => getPokemonData(search_term.value.toLowerCase()))
document.addEventListener('keypress',function(event)
{
if(event.key=='Enter')
{
getPokemonData(search_term.value);
}
})