forked from hanfarhanzoo/deacourse-team3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
90 lines (75 loc) · 2.21 KB
/
init.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
// declaring element
const username = document.getElementById("username")
const registerForm = document.getElementById("registerForm")
const logoutForm = document.getElementById("logoutForm")
const startSection = document.getElementById("start")
const box1 = document.getElementById("box1")
const box2 = document.getElementById("box2")
const box3 = document.getElementById("box3")
const rewardImage = document.getElementById("imgReward")
const title = document.getElementById("title")
const player = new Player()
let default_option = ['😍', '🤣', '😱']
box1.textContent = default_option[0]
box2.textContent = default_option[1]
box3.textContent = default_option[2]
function dice() {
let gacha = []
for (let i = 0; i < default_option.length; i++) {
const roll = default_option[~~(Math.random() * default_option.length)]
gacha.push(roll)
}
return gacha
}
function reward() {
fetch('https://zoo-animal-api.herokuapp.com/animals/rand').then(x => x.json()).then(result => {
//set nama hadiah reward
let text = document.createElement('h1')
text.textContent = result.name
//set gambar hadiah
let img = new Image(200, 200)
img.src = result.image_link
//set element
rewardImage.appendChild(img)
rewardImage.appendChild(text)
})
}
function winner() {
if (box1.textContent == box2.textContent && box1.textContent == box3.textContent) {
location.href = "#reward"
} else {
reward()
console.log('lose')
}
}
function start() {
//selama
const rolling = setInterval(function () {
const result = dice()
box1.textContent = result[0]
box2.textContent = result[1]
box3.textContent = result[2]
}, 100)
//ketika
setTimeout(function () {
clearInterval(rolling)
winner()
}, 3000)
}
onload = function () {
const token = sessionStorage.getItem('token')
if (token && token != null) {
registerForm.style.display = "none"
logoutForm.style.display = "block"
} else {
registerForm.style.display = "block"
logoutForm.style.display = "none"
}
}
function register() {
player.username = username.value
player.register
}
function logout() {
player.logout
}