-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
96 lines (75 loc) · 2.39 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
hud('Welcome to Rock, Paper, Scissors. Please make your selection.')
function cpuChoice () {
const choices = ["rock", "paper", "scissors"];
const getComputerInput= Math.floor(Math.random()* choices.length);
cpuAnswer = choices[getComputerInput];
//console.log(cpuAnswer)
return cpuAnswer;
}
function playRound (playerAnswer) {
if (playerscore>=5){
hud('You Win!')
} else if (cpuscore>=5){
hud('You Lose...better luck next time.')
}
else cpuChoice()
if (cpuAnswer==='rock'&& playerAnswer === 'scissors'){
hud('You Lose! Rock beats Scissors.')
cpuScore()
} else if (cpuAnswer==='scissors' && playerAnswer==='rock') {
hud('You Win! Rock beats Scissors')
playerScore()
} else if (cpuAnswer==='paper'&&playerAnswer==='rock'){
hud('You Lose! Paper beats Rock!')
cpuScore()
} else if (cpuAnswer==='rock'&& playerAnswer==='paper'){
hud('You Win! Paper beats Rock!')
playerScore()
} else if (cpuAnswer==='scissors'&& playerAnswer==='paper'){
hud('You Lose! Scissors beats Paper')
cpuScore()
} else if (cpuAnswer ==='paper' && playerAnswer==='scissors'){
hud('You Win! Scissors beats Paper')
playerScore()
} else if (cpuAnswer===playerAnswer) {
action = 'It\'s a Draw!'
}
}
function rock() {
let playerAnswer = 'rock';
playRound('rock');
}
function paper() {
let playerAnswer = 'paper';
playRound('paper');
//console.log(playerScore)
}
function scissors () {
let playerAnswer = 'scissors';
playRound('scissors');
//console.log(playerScore)
}
const rockbtn = document.querySelector('#rock');
const paperbtn = document.querySelector('#paper');
const scissorsbtn = document.querySelector('#scissors')
rockbtn.addEventListener('click',rock)
paperbtn.addEventListener('click', paper)
scissorsbtn.addEventListener('click', scissors)
let player1score = document.querySelector('#player1')
let pcScore = document.querySelector('#cpu')
let playerscore = 0
let cpuscore = 0
player1score.textContent = playerscore
pcScore.textContent = cpuscore
function playerScore() {
playerscore = playerscore+1
player1score.textContent = playerscore
}
function cpuScore() {
cpuscore = cpuscore+1
pcScore.textContent = cpuscore
}
function hud(action) {
const disp = document.querySelector('#actions')
disp.textContent = action
}