From e7c0e3c3e80383b9dbfaabc4f0ba79eb86a68f1f Mon Sep 17 00:00:00 2001 From: marleyrae Date: Mon, 19 Jun 2023 14:15:05 -0700 Subject: [PATCH] Write basic game --- index.html | 11 +++++++++++ index.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 index.html create mode 100644 index.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..dc9721f --- /dev/null +++ b/index.html @@ -0,0 +1,11 @@ + + + + + + Rock Paper Scissors + + + + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..2776e3d --- /dev/null +++ b/index.js @@ -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?'))