-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
96 lines (90 loc) · 2.61 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
//Storing html elements in js variables
const target = document.getElementById('target')
const player1 = document.getElementById('player1')
const player2 = document.getElementById('player2')
const reset = document.getElementById('reset')
const left = document.getElementById('left')
const right = document.getElementById('right')
const winner = document.getElementById('winner')
//Declaring global variables
let track = 0
let score = 0
let score1 = 0
//Eventlistener for player1 button
player1.addEventListener('click', () =>{
//if conditional for updating player1's score
if(target.value > 1){
track = target.value
target.disabled = true
score += 1
left.innerText = score
}
else{
alert('Select a number greater than 1')
}
//While loop for displaying the winner
while(score+score1 >= track && track > 1){
player1.disabled = true
player2.disabled = true
if(score > score1){
winner.innerText = "Player 1 won!"
left.style.color = "green"
right.style.color = "red"
}
else if(score < score1){
winner.innerText = "Player 2 won!"
left.style.color = "red"
right.style.color = "green"
}
else{
winner.innerText = "It's a draw!"
}
break
}
})
player2.addEventListener('click', () =>{
//if conditional for updating player1's score
if(target.value > 1){
track = target.value
target.disabled = true
score1 += 1
right.innerText = score1
}
else{
alert('Select a number greater than 1')
}
//While loop for displaying the winner
while(score+score1 >= track && track > 1){
player1.disabled = true
player2.disabled = true
if(score > score1){
winner.innerText = "Player 1 won!"
left.style.color = "green"
right.style.color = "red"
}
else if(score < score1){
winner.innerText = "Player 2 won!"
left.style.color = "red"
right.style.color = "green"
}
else{
winner.innerText = "It's a draw!"
}
break
}
})
//Eventlistener for the reset button
reset.addEventListener('click', () => {
winner.innerText = ''
right.innerText = 0
left.innerText = 0
score = 0
score1 = 0
player1.disabled = false
player2.disabled = false
target.disabled = false
track = 0
target.value = 0
left.style.color = ""
right.style.color = ""
})