-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (67 loc) · 2.61 KB
/
script.js
File metadata and controls
86 lines (67 loc) · 2.61 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
const scores = {'computerScore':0 , 'playerScore':0}
function getComputerChoice() {
let options = ['Rock','Paper','Scissors']
let n = Math.floor(Math.random()*3)
return options[n]
}
function getResult(playerChoice, computerChoice) {
// return the result of score based on if you won, drew, or lost
var score=0;
// All situations where human draws, set `score` to 0
if(playerChoice === computerChoice){
score = 0;
}else if(playerChoice==='Rock' && computerChoice==='Scissors'){
score = 1
}else if(playerChoice==='Paper' && computerChoice==='Rock'){
score = 1
}else if(playerChoice==='Scissors' && computerChoice==='Paper'){
score = 1
}else{
score = -1
}
return score
}
// ** showResult updates the DOM to `You Win!` or `You Lose!` or `It's a Draw!` based on the score. Also shows Player Choice vs. Computer Choice**
function showResult(score, playerChoice, computerChoice) {
const resultDiv = document.getElementById('result')
const handsDiv = document.getElementById('hands')
const playerScoreDiv = document.getElementById('player-score')
if(score == -1){
resultDiv.innerText = 'You Lose!'
}else if(score == 1){
resultDiv.innerText = 'You Win!'
}else{
resultDiv.innerText = 'Draw!'
}
handsDiv.innerText=`🧑🦰 ${playerChoice} vs 🤖 ${computerChoice}`
playerScoreDiv.innerText =` Your Score: ${scores['playerScore']}`
}
// ** Calculate who won and show it on the screen **
function onClickRPS(playerChoice) {
const computerChoice = getComputerChoice()
const score = getResult(playerChoice.value,computerChoice)
scores['playerScore'] += score
showResult(score, playerChoice.value,computerChoice)
}
// ** Make the RPS buttons actively listen for a click and do something once a click is detected **
function playGame() {
// use querySelector to select all RPS Buttons
const selection = document.querySelectorAll('.rpsButton')
selection.forEach(clickks =>{
clickks.onclick = () => onClickRPS(clickks)
})
// Add a click listener to the end game button that runs the endGame() function on click
let endGameButton = document.getElementById('endGameButton')
endGameButton.onclick = () => endGame()
}
// ** endGame function clears all the text on the DOM **
function endGame() {
let playerScore = document.getElementById('player-score')
let hands = document.getElementById('hands')
let result = document.getElementById('result')
playerScore.innerText = ''
hands.innerText = ''
result.innerText = ''
}
// ** endGame function clears all the text on the DOM **
playGame()