From fb18fc28b005834ebaeb719bb3ea8de65e7f5551 Mon Sep 17 00:00:00 2001 From: ak Date: Thu, 4 May 2023 12:24:55 -0700 Subject: [PATCH] final --- README.md | 2 ++ index.html | 10 +++++++ main.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 README.md create mode 100644 index.html create mode 100644 main.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..2869bd1 --- /dev/null +++ b/README.md @@ -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. \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..d9a69c1 --- /dev/null +++ b/index.html @@ -0,0 +1,10 @@ + + + + + + + Rock, Paper, Scissors + + + \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..559795a --- /dev/null +++ b/main.js @@ -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!") + } +})(); \ No newline at end of file