-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
59 lines (51 loc) · 1.99 KB
/
scripts.js
File metadata and controls
59 lines (51 loc) · 1.99 KB
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
let options = ['Rock', 'Paper', 'Scissors'];
const button_choice = document.querySelectorAll('button');
let player_score = 0;
let computer_score = 0;
const scores = document.querySelector('.scores')
const show_text = document.querySelector('.container')
let display = document.createElement('div')
let p_score = document.createElement('div')
p_score.textContent = `Player Score: 0`
let c_score = document.createElement('div')
c_score.textContent = `Computer Score: 0`
let final_score = document.createElement('div')
scores.appendChild(p_score)
scores.appendChild(c_score)
show_text.appendChild(display);
show_text.appendChild(final_score)
function getComputerChoice() {
return options[Math.floor(Math.random() * 3)];
}
function playRound(player_choice, computer) {
let computer_choice = computer();
let difference = options.indexOf(player_choice) - options.indexOf(computer_choice);
if (difference == 0) {
display.textContent = `Tied! ${player_choice} ties with ${computer_choice}`
return 0;
} else if (difference == 1 || difference == -2) {
display.textContent = `You Win! ${player_choice} beats ${computer_choice}`
return 1;
} else {
display.textContent = `You Lose! ${player_choice} loses to ${computer_choice}`
return -1;
}
}
function game() {
if (player_score < 5 && computer_score < 5) {
let score = playRound(this.textContent, getComputerChoice);
if (score == 1) {
player_score += 1
p_score.textContent = `Player Score: ${player_score}`
} else if (score == -1) {
computer_score += 1
c_score.textContent = `Computer Score: ${computer_score}`
}
if (player_score == 5) {
final_score.textContent = "Congratulations, you won the game!"
} else if (computer_score == 5) {
final_score.textContent = "Unfortunately, you lost the game."
}
}
}
button_choice.forEach((button) => {button.addEventListener('click', game)});