-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
59 lines (50 loc) · 1.79 KB
/
main.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
//Set global Constants
const options = ["rock", "paper", "scissors"]
let round = 1;
let playerScore = 0;
let computerScore = 0;
//Computer's Random Choice
function computerPlay() {
randNum = Math.floor(Math.random() * 3);
return options[randNum];
}
//One round of thg game
function playRound(playerSelection, computerSelection) {
// playerSelection = prompt('Rock, paper, or scissors?');
computerSelection = computerPlay();
console.log(playerSelection, computerSelection);
if (playerSelection === computerSelection) {
alert("It's a tie\! No points awarded\.");
} else if (playerSelection === "rock" && computerSelection === "scissors" ||
playerSelection === "paper" && computerSelection === "rock" ||
playerSelection === "scissors" && computerSelection === "paper") {
playerScore++;
$("#player-score").empty().append('Player Score \= ' + playerScore);
alert("You Win\! One point for player\.");
} else {
computerScore++;
$("#computer-score").empty().append('Computer Score \= ' + computerScore);
alert("You Lose\! One point for computer\.");
}
round++
$("#round").empty().append("Round\: " + round);
if (playerScore === 3 || computerScore === 3 ) {
if (playerScore === 3) {
alert("Congratulations\! You have defeated the computer\!");
}
else {
alert("Oh no\! You have been defeated by the computer\.");
}
} else {
return
}
}
//Initiates one play round on click of button
function play(e) {
let playerSelection = e.target.attributes[2].value;
let computerSelection = computerPlay();
playRound(playerSelection, computerSelection);
}
//Sets an event listener for click of button so play() can be called.
var buttons = document.querySelectorAll('.button');
buttons.forEach(button => button.addEventListener('click', play));