-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
87 lines (78 loc) · 2.36 KB
/
Copy pathscript.js
File metadata and controls
87 lines (78 loc) · 2.36 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
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
let round = 1;
let playerScore = 0;
let computerScore = 0;
const buttons = document.querySelectorAll('button');
const display = document.querySelector('.display');
const roundBoard = document.createElement('p');
const scoreBoard = document.createElement('p');
const resultBoard = document.createElement('p');
roundBoard.textContent = 'Round ' + round;
roundBoard.className = 'roundBoard';
scoreBoard.textContent = 'Player: ' + playerScore + ' VS ' + 'Computer: ' + computerScore;
scoreBoard.className = 'scoreBoard';
display.appendChild(roundBoard);
display.appendChild(scoreBoard);
display.appendChild(resultBoard);
buttons.forEach((button) => {
button.addEventListener('click', () => {
playRound(button.className);
round++;
roundBoard.textContent = 'Round ' + round;
scoreBoard.textContent = 'Player: ' + playerScore + ' VS ' + 'Computer: ' + computerScore;
checkGameScore();
});
});
function getComputerChoice() {
rand = Math.floor(Math.random() * 3);
if (rand == 0) {
return "rock";
}
else if (rand == 1) {
return "paper"
}
else {
return "scissors"
}
}
function playRound(playerSelection) {
let computerSelection = getComputerChoice();
let result;
if (playerSelection === computerSelection) {
result = "Draw! " + playerSelection + " and " + computerSelection + " tied.";
resultBoard.style.color = 'green';
}
else if ((playerSelection === "rock" && computerSelection === "scissors") ||
(playerSelection === "paper" && computerSelection === "rock") ||
(playerSelection === "scissors" && computerSelection === "paper")) {
result = "You win! " + playerSelection + " beats " + computerSelection;
resultBoard.style.color = 'blue';
playerScore++;
}
else {
result = "You lose! " + computerSelection + " beats " + playerSelection;
resultBoard.style.color = 'red';
computerScore++;
}
resultBoard.textContent = result;
console.log(result);
}
function checkGameScore() {
let finalResult;
if (computerScore >= 5 || playerScore >= 5) {
if (playerScore > computerScore) {
finalResult = "\nCongraturation!\nYou win this game!!!\n";
}
else if (playerScore === computerScore) {
finalResult = "\nDraw!\nThe computer was a hard game too.\nTry again!\n";
}
else {
finalResult = "\nYou lose.\nTry again!\n";
}
alert(finalResult);
console.log(finalResult);
playerScore = 0;
computerScore = 0;
round = 1;
resultBoard.textContent = '';
}
}