-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
113 lines (100 loc) · 2.37 KB
/
app.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//Mendaftarkan service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/ngitung/sw.js',{scope:'/ngitung/'}).then(function(reg){
//Berhasil terdaftar reg.scope
console.log('◕‿◕:' + reg.scope)
}).catch(function(error){
//Gagal Terdaftar error
console.log('ಠ_ಠ :' + error)
});
};
//Game
var soal = {x:0, y:0, jawab:0,opsi:0};
var lv = {max:10,min:0,l:1,skor:0,a:1};
//initialize
buatSoal();
dspLevel.innerHTML ="Level : " +lv.l;
outSkor.innerHTML = "Skor : " +lv.skor;
lanjut.addEventListener("click", function(){
buatSoal();
pause.style.display = 'none';
play.style.display = 'block';
});
btnJawab.addEventListener("click", function(){
if (soal.jawab == inpJawab.value) {
outHasil.innerHTML = "◕‿◕<br/>Benar";
lv.skor += 5;
if(lv.a >= 3){
lv.a = 1;
lv.l+=1;
if(lv.l > 1){
lv.max += 10;
lv.min += 10;
}
}
else lv.a +=1;
}
else {
lv.skor -= 2;
if (lv.skor <= 0) {
//gameover
outHasil.innerHTML = "Kamu Kalah!<br/> <strong>GAME OVER</strong>";
resetGame();
} else outHasil.innerHTML = "ಠ_ಠ<br/> Salah";
}
pause.style.display = 'block';
play.style.display = 'none';
outSkor.innerHTML = "Skor : " +lv.skor;
});
pilihan.onchange = function(){
soal.opsi = pilihan.value;
resetGame();
buatSoal();
}
//buat soal
function buatSoal(){
soal.x = acak(lv.max, lv.min);
soal.y = acak(lv.max, lv.min);
fopsi(soal.opsi);
dspLevel.innerHTML ="Level : " +lv.l;
}
function fopsi(a){
switch (a) {
case '1':
cekNilai(0);
soal.jawab = soal.x - soal.y;
outSoal.innerHTML = soal.x + " - " + soal.y;
break;
case '2':
soal.jawab = soal.x * soal.y;
outSoal.innerHTML = soal.x + " x " + soal.y;
break;
case '3':
cekNilai(1);
soal.jawab = soal.x / soal.y;
outSoal.innerHTML = soal.x +" : " + soal.y;
break;
default:
soal.jawab = soal.x + soal.y;
outSoal.innerHTML = soal.x + " + " + soal.y;
}
}
//buat random angka
function acak(max, min){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function cekNilai(a){
if(soal.x < soal.y){
var swap;
swap = soal.x;
soal.x = soal.y;
soal.y =swap;
}
}
function resetGame(){
lv.l = 1;
lv.skor = 0;
lv.a = 1;
lv.max = 10;
lv.min = 0;
}