-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini-game-oop.js
40 lines (32 loc) · 937 Bytes
/
mini-game-oop.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
class Player{
constructor(name, elementId){
this.name = name
this.elementId = elementId
}
randomScore(){
let score = Math.floor(Math.random() * 100 + 1)
let scoreElement = document.getElementById(this.elementId)
scoreElement.textContent = score
this.score = score
}
}
function start(){
let player = new Player("Player", "player-number")
player.randomScore()
let computer = new Player("Computer", "computer-number")
computer.randomScore()
if(player.score > computer.score){
// player menang
setWinner("Pemenangnya adalah Player")
}else if(player.score < computer.score){
// computer menang
setWinner("Pemenangnya adalah Computer")
}else{
// seri
setWinner("Permainan seri")
}
}
function setWinner(text){
let winner = document.getElementById("winner")
winner.textContent = text
}