-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
70 lines (63 loc) · 1.68 KB
/
Copy pathscript.js
File metadata and controls
70 lines (63 loc) · 1.68 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
function getComputerChoice(){
const temp=Math.random()
let choice;
if (temp>=0 && temp<(1/3)){
choice="ROCK";
}
else if (temp>=(1/3) && temp<(2/3)){
choice="PAPER";
}
else{
choice="SCISSORS";
}
return choice;
}
function getUserChoice(){
const choice=prompt("Enter your choice(Rock, Paper or Scissors):").toUpperCase();
return choice
}
let computerScore = 0;
let humanScore = 0;
let msg;
function playRound(humanChoice, computerChoice) {
if (((humanChoice === 'ROCK') & (computerChoice === 'SCISSORS')) ||
((humanChoice === 'SCISSORS') & (computerChoice === 'PAPER')) ||
((humanChoice === 'PAPER') & (computerChoice === 'ROCK'))) {
msg=`${humanChoice} beats ${computerChoice}. You win this round!`;
humanScore++
}
else if (humanChoice === computerChoice) {
msg=`You both chose ${humanChoice}. This round is a Tie!`;
}
else {
msg=`${computerChoice} beats ${humanChoice}. You lose this round!`;
computerScore++
}
alert(
`${msg}
===SCOREBOARD===
Player: ${humanScore}
Computer: ${computerScore}`);
console.log(
`${msg}
===SCOREBOARD===
Player: ${humanScore}
Computer: ${computerScore}`);
}
function playGame(){
for (let i=0; i<5;i++){
const humanSelection=getUserChoice();
const computerSelection=getComputerChoice();
playRound(humanSelection, computerSelection)
}
if (computerScore > humanScore) {
console.log('COMPUTER WINS THE GAME')
}
else if (humanScore > computerScore) {
console.log('PLAYER WINS THE GAME!')
}
else {
console.log('THE GAME WAS TIED!')
}
}
playGame()