-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
97 lines (78 loc) · 3 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
let playerScore = 0;
let computerScore = 0;
let roundCount = 0;
function computerPlay() {
let randNum = Math.floor(Math.random() * 13);
if (randNum <= 3) {
return "rock";
} else if (randNum > 3 && randNum <=7) {
return "paper";
} else if (randNum > 7) {
return "scissors";
}
}
function playRound(playerSelection, computerSelection) {
roundCount++;
const resultString = document.querySelector('p#resultsString');
const roundString = document.querySelector('p#round_counter');
playerSelection = playerSelection.toLowerCase();
if (roundCount < 6) {
console.log(roundCount);
//empty winner string
const winner = document.querySelector('p#winner');
winner.textContent="";
if (playerSelection == "rock") {
if (computerSelection == "paper") {
computerScore++;
resultString.textContent = `You Lose! Paper beats Rock`;
} else if (computerSelection == "scissors") {
playerScore++;
resultString.textContent = `You Win! Rock beats Scissors!`
} else {
resultString.textContent = `TIE!!! No one wins!`
}
} else if(playerSelection == "paper") {
if (computerSelection == "rock") {
playerScore++;
resultString.textContent= `You Win! Paper beats Rock`
} else if (computerSelection == "scissors") {
computerScore++;
resultString.textContent = `You Lose! Scissors beats Paper!`
} else {
resultString.textContent = `TIE!!! No one wins!`
}
} else if (playerSelection == "scissors") {
if (computerSelection == "paper") {
playerScore++;
resultString.textContent = `You win! Scissors beats paper`
} else if (computerSelection == "rock") {
computerScore++;
resultString.textContent = `You Lose! Rock beats Scissors!`
} else {
resultString.textContent = `TIE!!! No one wins!`
}
}
if (roundCount == 5) {
const winner = document.querySelector('p#winner');
if (playerScore > computerScore) {
winner.textContent= "Player wins the game";
} else {
winner.textContent = "Computer wins the game";
}
}
roundString.textContent = `Round ${roundCount} | Player Score: ${playerScore} | Computer Score: ${computerScore}`
}
}
const buttons = document.querySelectorAll('.choice_btn');
buttons.forEach((b) => {
b.addEventListener('click', (a) => {
if (roundCount < 5) {
playRound(b.id, computerPlay());
} else {
roundCount = 0;
playerScore = 0;
computerScore = 0;
playRound(b.id, computerPlay());
}
});
});