-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scissors.html
84 lines (68 loc) · 1.7 KB
/
Scissors.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
82
83
84
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p id="demo"></p>
<script>
function computerGame() {
let random = Math.floor(Math.random() * 3);
const options = ['scissors', 'rock', 'paper'];
return options[random];
}
function playerGame() {
let input = prompt('Type your choice - scissors, rock or paper');
input = input.toLowerCase();
return input
}
function playRound (pl, com) {
if (pl === com) {
return 'Tie!'
} else if (pl === 'scissors' && com === 'paper'){
return 1
} else if (pl === 'paper' && com === 'rock') {
return 1
} else if (pl === 'rock' && com === 'scissors') {
return 1
} else if (pl === 'scissors' && com === 'rock'){
return 0
} else if (pl === 'paper' && com === 'scissors') {
return 0
} else if (pl === 'rock' && com === 'paper') {
return 0
} else {
return 2
}
}
function game() {
let playerScore = 0;
let computerScore = 0;
let playerSelect;
let computerSelect;
let roundResult;
for (let i = 0; i < 5; i++) {
playerSelect = playerGame();
computerSelect = computerGame();
roundResult = playRound(playerSelect, computerSelect);
if (roundResult === 1) {
playerScore++
} else if (roundResult === 0) {
computerScore++
}
}
if (playerScore > computerScore) {
return (`You won with score ${playerScore} : ${computerScore}`)
} else if (playerScore < computerScore) {
return (`You lost with score ${playerScore} : ${computerScore}`)
} else {
return ("It's a TIE!")
}
}
// let final = console.log(game());
let final = game();
document.getElementById("demo").innerHTML = final;
</script>
</body>
</html>