-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathog.html
81 lines (74 loc) · 2.77 KB
/
og.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
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html>
<head>
<title>Rock Paper Scissors</title>
<meta charset="UTF-8"/>
</head>
<body>
<script>
const myArray = [
'Rock',
'Paper',
'Scissors'
];
function computerPlay() {
return myArray[~~(Math.random()*myArray.length)]
}
let computerSelection;
let playerSelection;
let computerScore;
let playerScore;
game();
function game () {
computerScore=0;
playerScore=0;
for (let i=0; i<5; i++) {
onUserInput();
}
console.log(declareWinner());
function declareWinner() {
if (playerScore===computerScore) {
alert(playerScore + '-' + computerScore + '\nTie game!');
return playerScore + '-' + computerScore + '\nTie game!';
} else if (playerScore>computerScore) {
alert(playerScore + '-' + computerScore + '\nYou win!!');
return playerScore + '-' + computerScore + '\nYou win!!';
} else {
alert(playerScore + '-' + computerScore + '\nYou lost. Better luck next time!');
return playerScore + '-' + computerScore + '\nYou lost. Better luck next time!';
}
}
}
function onUserInput() {
playerSelection = prompt('Rock, Paper, or Scissors?', '');
if ((playerSelection.toLowerCase()=='rock')
||(playerSelection.toLowerCase()=='paper')
||(playerSelection.toLowerCase()=='scissors')) {
console.log(playRound(playerSelection,computerSelection));
} else {
alert('That\'s not a valid choice.');
onUserInput();
}
}
function playRound(playerSelection, computerSelection) {
computerSelection = computerPlay().toLowerCase();
playerSelection = playerSelection.toLowerCase();
if (computerSelection==playerSelection) {
alert('Tie game!');
return 'Tie game\nComputer Score: ' +
computerScore + '\nYour Score: ' + playerScore;
} else if ((computerSelection=='rock' && playerSelection=='scissors')
|| (computerSelection=='scissors' && playerSelection=='paper')
|| (computerSelection=='paper' && playerSelection=='rock')) {
alert('You lose! ' + computerSelection + ' beats ' + playerSelection);
return 'Computer Score: ' + ++computerScore +
'\nYour Score: ' + playerScore;
} else {
alert('You win! ' + playerSelection + ' beats ' + computerSelection);
return 'Your Score: ' + ++playerScore +
'\nComputer Score: ' + computerScore;
}
}
</script>
</body>
</html>