-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst.html
60 lines (54 loc) · 2.05 KB
/
first.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rock-Paper-Scissors</title>
</head>
<body>
<script>
const choices = ['rock', 'paper', 'scissors'];
function getComputerChoice() {
let randomNum = choices[Math.floor(Math.random() * choices.length)];
return randomNum;
}
const computerSelection = getComputerChoice();
const playerSelection = prompt('Enter Game: ').toLowerCase();
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
alert("It's a tie!");
} else if (playerSelection === 'rock' && computerSelection === 'paper' ||
playerSelection === 'paper' && computerSelection === 'scissors' ||
playerSelection === 'scissors' && computerSelection === 'rock') {
computerScore++;
alert(`You lose! ${computerSelection} beats ${playerSelection}`);
} else if (playerSelection === 'paper' && computerSelection === 'rock' ||
playerSelection === 'scissors' && computerSelection === 'paper' ||
playerSelection === 'rock' && computerSelection === 'paper') {
playerScore++;
alert(`You win! ${playerSelection} beats ${computerSelection}`);
} else {
alert('Please enter valid choice');
}
}
function playGame() {
playerScore = 0;
computerScore = 0;
for(let i = 0; i < 5; i++) {
const computerSelection = getComputerChoice();
const playerSelection = prompt('Enter Game: ').toLowerCase();
console.log(playRound(playerSelection, computerSelection));
}
if(playerScore > computerScore) {
alert('You win!');
} else if(playerScore < computerScore) {
alert('You lose!');
} else {
alert("It's a tie!");
}
}
playGame();
// test line console.log(playRound(playerSelection, computerSelection));
</script>
</body>
</html>