-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
74 lines (53 loc) · 1.96 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
function getPilihanComputer() {
const comp = Math.random();
if( comp < 0.34 ) return 'gajah';
if( comp >= 0.34 && comp < 0.67 )return 'orang';
return 'semut';
}
function getHasil(comp, player){
if( player == comp ) return 'SERI!';
if( player == 'gajah' ) return ( comp == 'orang' ) ? 'MENANG!' : 'KALAH!';
if( player == 'orang' ) return ( comp == 'gajah' ) ? 'KALAH!' : 'MENANG!';
if( player == 'semut' ) return ( comp == 'orang' ) ? 'KALAH' : 'MENANG!';
}
function putar(){
const imgComputer = document.querySelector('.img-komputer');
const gambar = ['gajah','orang','semut'];
let i = 0;
const waktuMulai = new Date().getTime();
setInterval(function(){
if(new Date().getTime()- waktuMulai > 1000 ){
clearInterval;
return;
}
imgComputer.setAttribute('src','img/' + gambar[i++]+ '.png');
if(i == gambar.length){
i = 0;
}
},100)
}
const pilihan = document.querySelectorAll('li img');
pilihan.forEach(function(pil){
pil.addEventListener('click', function(){
const pilihanComputer = getPilihanComputer();
const pilihanPlayer = pil.className;
const hasil = getHasil(pilihanComputer, pilihanPlayer);
putar();
setTimeout(function(){
const imgComputer = document.querySelector('.img-komputer');
imgComputer.setAttribute('src','img/' + pilihanComputer + '.png');
const info = document.querySelector('.info');
info.innerHTML = hasil;
}, 1000);
});
});
const pGajah = document.querySelector('.gajah');
pGajah.addEventListener('click', function(){
const pilihanComputer = getPilihanComputer();
const pilihanPlayer = pGajah.className;
const hasil = getHasil(pilihanComputer, pilihanPlayer);
const imgComputer = document.querySelector('.img-komputer');
imgComputer.setAttribute('src','img/' + pilihanComputer + '.png');
const info = document.querySelector('.info');
info.innerHTML = hasil;
});