-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
77 lines (59 loc) · 2.24 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
//https://superheroapi.com/api/access-token/character-id
//If u search by name ---> base_URL/search/name
//then u will retrive image as json.results[0].image.url
//here the response will be an array of results
//But if u serch by ID ---> base_URL/id
//then u will retrive image as json.image.url
//here the response will be a single object
const newHeroButton = document.getElementById('newHeroButton')
const heroImageDiv = document.getElementById('heroImage')
const searchButtonDiv = document.getElementById('searchButton')
const searchInputDiv = document.getElementById('searchInput')
const nameDiv = document.getElementById('name')
const SUPERHERO_TOKEN = '10223569763528853'
const BASE_URL = `https://superheroapi.com/api.php/${SUPERHERO_TOKEN}`
const getRandomSuperHero = (id, name) => {
fetch(`${BASE_URL}/${id}`)
.then(response => response.json())
.then(json => {
//console.log(json)
const superHero = json
showHeroInfo(superHero)
})
}
const getSuperHeroBySearch = (name) => {
fetch(`${BASE_URL}/search/${name}`)
.then(response => response.json())
.then(json => {
console.log(json)
const hero = json.results[0]
showHeroInfo(hero)
})
}
const randomNumber = () => {
const noOfHeroes = 731
return Math.floor(Math.random() * noOfHeroes) + 1;
}
const statToEmoji = {
intelligence: '🧠',
strength: '💪',
speed: '⚡',
durability: '🏋️♂️',
power: '📊',
combat: '⚔️',
}
const showHeroInfo = (character) => {
const name = `<h1 style="padding-bottom:0px;">${character.name}</h1><hr>`
const img = `<img src='${character.image.url}' width="80%" />`
//console.log(Object.keys(character.powerstats))
//Object.keys will return an array of keys
const stats = Object.keys(character.powerstats).map(stat => {
return `<p>${statToEmoji[stat]} ${stat.toUpperCase()} :${character.powerstats[stat]}</p>`
}).join('')
heroImageDiv.innerHTML = `${img}`
nameDiv.innerHTML = `${name}${stats}`
//console.log(stats.join(''))
//return stats.join('')
}
newHeroButton.onclick = () => getRandomSuperHero(randomNumber())
searchButtonDiv.onclick = () => getSuperHeroBySearch(searchInputDiv.value)