-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRock-Paper-Scissors-functions.html
More file actions
58 lines (44 loc) · 1.34 KB
/
Rock-Paper-Scissors-functions.html
File metadata and controls
58 lines (44 loc) · 1.34 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
<!DOCTYPE html>
<html>
<head>
<title>Rock Paper Scissors</title>
</head>
<body>
<p>Rock Paper Scissors</p>
<button onclick="
playGame('rock');
">Rock</button>
<button onclick="
playGame('paper');
">paper</button>
<button onclick="
playGame('scissors');
">Scissors</button>
<script>
function playGame(playerMove) {
const computerMove=pickComputerMove();
let result=''
if (computerMove === 'rock') {
result = 'You lose.';
} else if (computerMove === 'paper') {
result = 'You win.';
} else if (computerMove === 'scissors') {
result = 'Tie.';
}
alert(`You picked ${playerMove}. Computer picked ${computerMove}. ${result}`);
}
function pickComputerMove() {
const randomNumber = Math.random();
let computerMove = '';
if (randomNumber >= 0 && randomNumber < 1 / 3) {
computerMove = 'rock';
} else if (randomNumber >= 1 / 3 && randomNumber < 2 / 3) {
computerMove = 'paper';
} else if (randomNumber >= 2 / 3 && randomNumber < 1) {
computerMove = 'scissors';
}
return computerMove;
}
</script>
</body>
</html>