Last active
February 8, 2024 13:01
-
-
Save gitdagray/38322a7141f4236e050086e7053febb7 to your computer and use it in GitHub Desktop.
Rock_Paper_Scissors_v1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your First Interactive Game | |
let playGame = confirm("Shall we play rock, paper, or scissors?"); | |
if (playGame) { | |
//play | |
let playerChoice = prompt("Please enter rock, paper, or scissors."); | |
if (playerChoice) { | |
let playerOne = playerChoice.trim().toLowerCase(); | |
if ( | |
playerOne === "rock" || | |
playerOne === "paper" || | |
playerOne === "scissors" | |
) { | |
let computerChoice = Math.floor(Math.random() * 3 + 1); | |
let computer = | |
computerChoice === 1 | |
? "rock" | |
: computerChoice === 2 | |
? "paper" | |
: "scissors"; | |
let result = | |
playerOne === computer | |
? "Tie game!" | |
: playerOne === "rock" && computer === "paper" | |
? `playerOne: ${playerOne}\nComputer: ${computer}\nComputer wins!` | |
: playerOne === "paper" && computer === "scissors" | |
? `playerOne: ${playerOne}\nComputer: ${computer}\nComputer wins!` | |
: playerOne === "scissors" && computer === "rock" | |
? `playerOne: ${playerOne}\nComputer: ${computer}\nComputer wins!` | |
: `playerOne: ${playerOne}\nComputer: ${computer}\nplayerOne wins!`; | |
alert(result); | |
let playAgain = confirm("Play Again?"); | |
playAgain ? location.reload() : alert("Ok, thanks for playing."); | |
} else { | |
alert("You didn't enter rock, paper, or scissors."); | |
} | |
} else { | |
alert("I guess you changed your mind. Maybe next time."); | |
} | |
} else { | |
alert("Ok, maybe next time."); | |
} |
coming from python ,it's really hard to keep track of which else related to what if. I think the else block related to checking the proper input needs a location.reload(); as our dear friend above stated
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
else {
alert("You didn't enter rock, paper, or scissors.");
location.reload();
}