-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
57 lines (53 loc) · 1.43 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
const progress = document.getElementById('progress')
const prev = document.getElementById('prev')
const next = document.getElementById('next')
const area = document.querySelectorAll('.area')
const jokeEl = document.getElementById('joke')
let currentActive = 1
next.addEventListener('click', () => {
currentActive++
if (currentActive > area.length) {
currentActive = area.length
}
update()
})
prev.addEventListener('click', () => {
currentActive--
if (currentActive < 1) {
currentActive = 1
}
update()
})
function update() {
area.forEach((area, idx) => {
if (idx < currentActive) {
area.classList.add('active')
} else {
area.classList.remove('active')
}
})
const actives = document.querySelectorAll('.active')
progress.style.width = ((actives.length - 1) / (area.length - 1)) * 100 + '%'
if (currentActive === 1) {
prev.disabled = true
} else if (currentActive === area.length) {
next.disabled = true
} else {
prev.disabled = false
next.disabled = false
}
}
next.addEventListener('click', generateJoke)
prev.addEventListener('click', generateJoke)
generateJoke()
//Using ASYNC / AWAIT
async function generateJoke() {
const config = {
headers: {
Accept: 'application/json',
},
}
const res = await fetch('https://icanhazdadjoke.com/', config)
const data = await res.json()
jokeEl.innerHTML = data.joke
}