-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
55 lines (49 loc) · 1.9 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
44
45
46
47
48
49
50
51
52
53
54
const randomSuperHero = document.getElementById('getSuperhero');
const heroImageDiv = document.getElementById('heroImage');
const searchButton = document.getElementById('searchButton');
const searchedHero = document.getElementById('hero');
const numberOfHeroes = 733;
const SUPERHERO_TOKEN = '3492498610968981';
const BASE_URL = `https://superheroapi.com/api.php/${SUPERHERO_TOKEN}`;
const displaySuperHero = async (superhero) => {
const url = `${BASE_URL}/${superhero.id}/powerstats`
const name = `<p>Hero Name: ${superhero.name}</p>`;
searchedHero.value = ''
heroImageDiv.innerHTML =
`${name}<img src="${superhero.image.url}" alt='' height = 100 width = 100 />`
const response = await fetch(url)
const data = await response.json()
heroImageDiv.innerHTML +=
`<p>Powerstats<br><br>
Intelligence: ${data.intelligence}<br>
Strength: ${data.strength}<br>
Speed: ${data.speed}<br>
Power: ${data.power}<br>
Combat: ${data.combat}<br>
Durability: ${data.durability}
</p>`
}
const getSuperHeroById = async (id) => {
const url = `${BASE_URL}/${id}`
const response = await fetch(url)
const data = await response.json()
displaySuperHero(data)
}
const searchSuperHeroByName = async (name) => {
const url = `${BASE_URL}/search/${name}`
const response = await fetch(url)
const data = await response.json()
const results = data.results;
if (!results || results.length === 0) {
heroImageDiv.innerHTML = `<p>Can't find the searched superhero.</p>`;
return
}
const superhero = results[0]
displaySuperHero(superhero)
}
const getRandomSuperHero = () => {
const randomId = Math.floor(Math.random() * numberOfHeroes) + 1;
getSuperHeroById(randomId)
}
randomSuperHero.onclick = getRandomSuperHero;
searchButton.onclick = () => searchSuperHeroByName(searchedHero.value)