-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (52 loc) · 2.01 KB
/
script.js
File metadata and controls
68 lines (52 loc) · 2.01 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
// https://superheroapi.com/api/access-token/character-id
const SUPERHERO_TOKEN = '10223569763528853'
const BASE_URL = `https://superheroapi.com/api.php/${SUPERHERO_TOKEN}`
const newHeroButton = document.getElementById('newHeroButton')
const heroImageDiv = document.getElementById('heroImage')
const searchButton = document.getElementById('searchButton')
const searchInput = document.getElementById('searchInput')
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
showHeroInfo(superHero)
})
}
const statToEmoji = {
intelligence: '🧠',
strength: '💪',
speed: '⚡',
durability: '🏋️♂️',
power: '📊',
combat: '⚔️',
}
const showHeroInfo = (character) => {
const name = `<h2>${character.name}</h2>`
const img = `<img src="${character.image.url}" height=200 width=200/>`
const stats = Object.keys(character.powerstats).map(stat => {
return `<p>${statToEmoji[stat]} ${stat.toUpperCase()}: ${character.powerstats[stat]}</p>`
}).join('')
heroImageDiv.innerHTML = `${name}${img}${stats}`
}
// <p>💪 Strength: ${json.powerstats.strength}</p><p>🧠 Intelligence: ${json.powerstats.intelligence}</p><p>🧠 Combat: ${json.powerstats.intelligence}</p><p>🧠 Speed: ${json.powerstats.intelligence}</p><p>🧠 Durability: ${json.powerstats.intelligence}</p>
const getSearchSuperHero = (name) => {
console.log(searchInput.value)
fetch(`${BASE_URL}/search/${name}`)
.then(response => response.json())
.then(json => {
const hero = json.results[0]
showHeroInfo(hero)
})
}
const randomHero = () => {
const numberOfHeroes = 731
return Math.floor(Math.random() * numberOfHeroes) + 1
}
newHeroButton.onclick = () => getSuperHero(randomHero())
searchButton.onclick = () => getSearchSuperHero(searchInput.value)