-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
97 lines (73 loc) · 2.94 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 humanScore = 0
let computerScore = 0
const options = document.querySelector("#options")
const scoreboard = document.querySelector("#results")
const humanChoiceDisp = document.createElement("div")
const computerChoiceDisp = document.createElement("div")
const matchResults = document.createElement("div")
const humanScoreDisp = document.createElement("div")
const computerScoreDisp = document.createElement("div")
const winnerDisp = document.createElement("div")
function displayScores () {
computerScoreDisp.textContent = `Current Comptuer Score: ${computerScore}`
humanScoreDisp.textContent = `Current Human Score: ${humanScore}`
scoreboard.appendChild(humanChoiceDisp)
scoreboard.appendChild(computerChoiceDisp)
scoreboard.appendChild(matchResults)
scoreboard.appendChild(computerScoreDisp)
scoreboard.appendChild(humanScoreDisp)
scoreboard.appendChild(winnerDisp)
}
function getComputerChoice () {
let choices = ["rock", "paper", "scissors"]
let selection = choices[Math.floor(Math.random() * choices.length)]
computerChoiceDisp.textContent = `Computer picked ${selection}!`
return selection
}
function getHumanChoice () {
let humanChoice = prompt("Pick an option: rock, paper or scissors").toLowerCase()
console.log(humanChoice)
if (humanChoice != "rock" && humanChoice != "paper" && humanChoice != "scissors") {
getHumanChoice()
}
return humanChoice
}
function playRound (humanChoice, computerChoice) {
if (humanChoice == computerChoice) {
matchResults.textContent = "It's a tie!"
} else if ((humanChoice == "paper" && computerChoice == "scissors")
|| (humanChoice == "scissors" && computerChoice == "rock")
|| (humanChoice == "rock" && computerChoice == "paper")) {
matchResults.textContent = "You lose!"
computerScore++;
} else if ((humanChoice == "paper" && computerChoice == "rock")
|| (humanChoice == "scissors" && computerChoice == "paper")
|| (humanChoice == "rock" && computerChoice == "scissors")) {
matchResults.textContent = "You win!"
humanScore++;
}
return humanScore, computerScore
}
options.addEventListener("click", (event) => {
let target = event.target
switch(target.id) {
case "rock":
playRound("rock", getComputerChoice())
humanChoiceDisp.textContent = `You picked ${target.id}`
break;
case "paper":
playRound("paper", getComputerChoice())
humanChoiceDisp.textContent = `You picked ${target.id}`
break;
case "scissors":
playRound("scissors", getComputerChoice())
humanChoiceDisp.textContent = `You picked ${target.id}`
break;
}
if (humanScore == 5) {
winnerDisp.textContent = "Player won"
} else if (computerScore == 5) {
winnerDisp.textContent = "Computer won"
}
displayScores()
})