-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
80 lines (73 loc) · 2.67 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
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
function getComputerChoice(){
let choices = ["Rock", "Paper", "Scissors"]
return choices[getRandomInt(3)]
}
function playRound(playerSelection, computerSelection) {
// your code here!
playerSelection = playerSelection.toLowerCase()
computerSelection = computerSelection.toLowerCase()
if(playerSelection == "rock"){
if(computerSelection == "rock"){
return "Tie!"
}else if(computerSelection == "paper"){
scores[1]++
return "You Lose! Paper beats Rock"
}else if(computerSelection == "scissors"){
scores[0]++;
return "You Win! Rock beats Scissors"
}
}
if(playerSelection == "paper"){
if(computerSelection == "rock"){
scores[0]++
return "You Win! Paper beats Rock"
}else if(computerSelection == "paper"){
return "Tie!"
}else if(computerSelection == "scissors"){
scores[1]++
return "You Lose! Scissors beats Paper"
}
}
if(playerSelection == "scissors"){
if(computerSelection == "rock"){
scores[1]++
return "You Lose! Rock beats Scissors"
}else if(computerSelection == "paper"){
scores[0]++;
return "You Win! Scissors beats paper"
}else if(computerSelection == "scissors"){
return "Tie!"
}
}
}
function game(){
const playerSelection = playerSelect()
const computerSelection = getComputerChoice();
console.log(playRound(playerSelection, computerSelection));
}
function playerSelect(){
let playerSelection = window.prompt("Rock, Paper, or Scissors").toLowerCase()
let choices = ["rock", "paper", "scissors"]
if(!choices.includes(playerSelection)){
playerSelect()
}else{
return playerSelection;
}
}
let scores = [0, 0]
let results = document.querySelector("#results")
document.querySelector("#buttons").addEventListener('click', function(e){
document.querySelector("#result").innerHTML = playRound(e.target.alt, getComputerChoice())
if(scores[0] == 5){
results.style.fontSize = "400%"
results.innerHTML = "YOU WIN!<br><button id='restart'>Click Here to Restart</button>"
}else if(scores[1] == 5){
results.style.fontSize = "400%"
results.innerHTML = "COMPUTER WINS :(<br><button id='restart' onClick='window.location.reload();'>Click Here to Restart</button>"
}
document.querySelector("#playerScore").innerHTML = "Your score is: " + scores[0]
document.querySelector("#computerScore").innerHTML = "The computer's score is: " + scores[1]
})