-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
70 lines (66 loc) · 2.64 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<script>
function game() {
let playerScore=0;
let computerScore=0;
for (let i=0; i<5; i++) {
let playerSelection=prompt('Rock, Paper or Scissors?');
const computerSelection=computerPlay();
let result=playRound(playerSelection,computerSelection);
console.log(result);
if (result.includes('Win')) {
playerScore++;
} else if (result.includes('Lose')) {
computerScore++;
}
}
if (playerScore>computerScore) {
console.log('Player wins!');
} else if (computerScore>playerScore) {
console.log('Computer wins!');
} else {
console.log('It\'s a tie!');
}
}
function playRound(playerSelection, computerSelection) {
playerSelection=playerSelection.toUpperCase();
if (playerSelection=='ROCK' && computerSelection=='Paper') {
return 'You Lose! Paper beats Rock';
}
else if (playerSelection=='ROCK' && computerSelection=='Scissors') {
return 'You Win! Rock beats Scissors';
}
else if (playerSelection=='SCISSORS' && computerSelection=='Rock') {
return 'You Lose! Rock beats Scissors';
}
else if (playerSelection=='SCISSORS' && computerSelection=='Paper') {
return 'You Win! Scissors beats Paper';
}
else if (playerSelection=='PAPER' && computerSelection=='Rock') {
return 'You Win! Paper beats Rock';
}
else if (playerSelection=='PAPER' && computerSelection=='Scissors') {
return 'You Lose! Scissors beats Paper';
}
else if (playerSelection==computerSelection.toUpperCase()) {
return 'It\'s a draw!';
}
}
function computerPlay() {
let x=Math.floor((Math.random() * 3) + 1);
switch (x) {
case 1:
return 'Rock';
break;
case 2:
return 'Paper';
break;
case 3:
return 'Scissors';
}
}
</script>
</head>
</html>