-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
67 lines (56 loc) · 2.42 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
let player;
let computer;
let playerScore = 0;
let computerScore = 0;
const playerChoice = document.querySelectorAll(".playerChoice"); //Gets all class name playersChoice.
const playerText = document.getElementById("playerText");
const computerText = document.getElementById("computerText");
const resultText = document.getElementById("resultText");
const computerScoreText = document.getElementById("computerScore");
const playerScoreText = document.getElementById("playerScore");
/*Randomize between Rock, Paper, Scissors for computer choice*/
const getComputerChoice = () =>{
const computerOptions =['Rock','Paper','Scissors']; //An array that gives three choices of Rock,Paper, and Scissors
const computerSelection = computerOptions[Math.floor(Math.random()*computerOptions.length)]; //Cycles through the array to get random computer choices
return computerSelection;
//console.log (computerSelection);
};
/*Display player choice and computer choice */
playerChoice.forEach(button => button.addEventListener("click", () => { //When button click getComputerChoice is active.
player = button.textContent;
computer = getComputerChoice();
playerText.textContent = `Player: ${player}`;
computerText.textContent = `Computer: ${computer}`;
resultText.textContent = playround();
computerScoreText.textContent=`Computer Score: ${computerScore}`;
playerScoreText.textContent=`Player Score: ${playerScore}`;
}));
/* plays a round base on the Rock Paper Scissor rules */
function playround(){
if((computer == 'Rock' && player == 'Paper')||
(computer == 'Paper' && player == 'Scissors')||
(computer == 'Scissors' && player == 'Rock')){
playerScore+=1;
if (playerScore == 5){
disableButton();
return "You Win! Refresh the page to play again."
}
return("You won this round");
}
else if(computer == player) {
return "Draw";
}else{
computerScore+=1;
if(computerScore == 5){
disableButton();
return "Game Over! You Lose. Refresh the page to play again."
}
return ("You lose this round");
}
}
//Function that disable the button from being click
function disableButton(){
document.getElementById("rockChoice").disabled = true;
document.getElementById("paperChoice").disabled = true;
document.getElementById("scissorChoice").disabled = true;
}