Write basic game
This commit is contained in:
parent
17a2b2a65d
commit
e7c0e3c3e8
2 changed files with 56 additions and 0 deletions
11
index.html
Normal file
11
index.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
<!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 src="index.js"></script>
|
||||
</body>
|
||||
</html>
|
45
index.js
Normal file
45
index.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
const getComputerChoice = function () {
|
||||
const choices = ['rock', 'paper', 'scissors']
|
||||
return choices[Math.floor(Math.random() * choices.length)]
|
||||
}
|
||||
|
||||
const playRound = function (playerSelection, computerSelection) {
|
||||
playerSelection = playerSelection.toLowerCase()
|
||||
let playerWon = false
|
||||
|
||||
if (playerSelection === computerSelection) {
|
||||
return `Tie: both picked ${playerSelection}.`
|
||||
}
|
||||
|
||||
switch (playerSelection) {
|
||||
case 'rock':
|
||||
playerWon = computerSelection === 'scissors'
|
||||
break
|
||||
|
||||
case 'paper':
|
||||
playerWon = computerSelection === 'rock'
|
||||
break
|
||||
|
||||
case 'scissors':
|
||||
playerWon = computerSelection === 'paper'
|
||||
break
|
||||
}
|
||||
|
||||
return playerWon
|
||||
}
|
||||
|
||||
const game = function (playerSelection) {
|
||||
let playerScore = 0
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const result = playRound(playerSelection, getComputerChoice())
|
||||
|
||||
if (result) {
|
||||
playerScore++
|
||||
}
|
||||
}
|
||||
|
||||
document.write(`Your score: ${playerScore}`)
|
||||
}
|
||||
|
||||
game(prompt('Rock, paper, or scissors?'))
|
Loading…
Reference in a new issue