This commit is contained in:
ak 2023-05-04 12:24:55 -07:00
commit fb18fc28b0
3 changed files with 90 additions and 0 deletions

2
README.md Normal file
View file

@ -0,0 +1,2 @@
# js-rock-paper-scissors
A very basic rock-paper-scissors proof-of-concept game coded in JavaScript. Asks user for user input, then compares it to a randomized value for the computer. Best out of 5 rounds.

10
index.html Normal file
View file

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rock, Paper, Scissors</title>
<script src="main.js"></script>
</head>
</html>

78
main.js Normal file
View file

@ -0,0 +1,78 @@
const ROCK = "rock";
const PAPER = "paper";
const SCISSORS = "scissors";
let ties = 0;
function getComputerChoice () {
let x = Math.floor(Math.random() * 3);
if (x === 1) {
return ROCK;
}
else if (x === 2) {
return PAPER;
}
else {
return SCISSORS;
}
}
function playRound () {
const computerSelection = getComputerChoice();
const playerSelection = (prompt("Rock, Paper or Scissors?")).toLowerCase();
if (playerSelection === computerSelection) {
alert("It's a tie!");
ties++;
}
else if (playerSelection === ROCK) {
if (computerSelection === PAPER) {
alert("You Lose! Paper beats Rock!");
return 0;
}
else {
alert("You Win! Rock beats Scissors!");
return 1;
}
}
else if (playerSelection === PAPER) {
if (computerSelection === ROCK) {
alert("You Win! Paper beats Rock!");
return 1;
}
else {
alert("You Lose! Scissors beat Paper!");
return 0;
}
}
else if (playerSelection === SCISSORS) {
if (computerSelection === ROCK) {
alert("You Lose! Rock beats Scissors!");
return 0;
}
else {
alert("You Win! Scissors beat Paper!");
return 1;
}
}
else {
alert("Invalid input!")
}
}
(function game() {
let wins = 0;
for (let i = 0; i < 5; i++) {
wins += playRound();
}
if (ties === 5) {
alert("You tied!")
return;
}
let losses = 5 - wins - ties;
if ( wins > losses) {
alert("You win!");
return;
}
else {
alert("You lose!")
}
})();