-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
99 lines (86 loc) · 2.62 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const SUPERHERO_TOKEN = '10223569763528853'
const BASE_URL = `https://superheroapi.com/api.php/${SUPERHERO_TOKEN}`
const newHeroButton = document.getElementById('random');
const searchButton = document.getElementById('search-btn')
const searchInput = document.getElementById('form-control')
const showSuperheroDetails = (data) => {
console.log(data);
document.querySelector('.app-body-content-thumbnail').innerHTML = `
<img src = "${data.image.url}">
`;
document.querySelector('.name').textContent = data.name;
document.querySelector('.powerstats').innerHTML = `
<li>
<div>
<i class = "fa-solid fa-shield-halved"></i>
<span>intelligence</span>
</div>
<span>${data.powerstats.intelligence}</span>
</li>
<li>
<div>
<i class = "fa-solid fa-shield-halved"></i>
<span>strength</span>
</div>
<span>${data.powerstats.strength}</span>
</li>
<li>
<div>
<i class = "fa-solid fa-shield-halved"></i>
<span>speed</span>
</div>
<span>${data.powerstats.speed}</span>
</li>
<li>
<div>
<i class = "fa-solid fa-shield-halved"></i>
<span>durability</span>
</div>
<span>${data.powerstats.durability}</span>
</li>
<li>
<div>
<i class = "fa-solid fa-shield-halved"></i>
<span>power</span>
</div>
<span>${data.powerstats.power}</span>
</li>
<li>
<div>
<i class = "fa-solid fa-shield-halved"></i>
<span>combat</span>
</div>
<span>${data.powerstats.combat}</span>
</li>
`;
}
const getSuperHero = (id, name) => {
// name 👉 base_url/search/batman
// json.results[0].image.url
// id: 👉 base_url/id
// json.image.url
fetch(`${BASE_URL}/${id}`)
.then(response => response.json())
.then(json => {
console.log(json.powerstats)
const superHero = json
showSuperheroDetails(superHero)
})
}
const getSearchSuperHero = (name) => {
console.log(searchInput.value)
searchInput.value=""
fetch(`${BASE_URL}/search/${name}`)
.then(response => response.json())
.then(json => {
const hero = json.results[0]
console.log(hero)
showSuperheroDetails(hero)
})
}
const randomHero = () => {
const numberOfHeroes = 731
return Math.floor(Math.random() * numberOfHeroes) + 1
}
searchButton.onclick = () => getSearchSuperHero(searchInput.value)
newHeroButton.onclick = () => getSuperHero(randomHero())