Skip to content

Instantly share code, notes, and snippets.

@gitdagray
Last active February 8, 2024 13:01
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save gitdagray/38322a7141f4236e050086e7053febb7 to your computer and use it in GitHub Desktop.
Save gitdagray/38322a7141f4236e050086e7053febb7 to your computer and use it in GitHub Desktop.
Rock_Paper_Scissors_v1
// 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.");
}
@kalkerisharat
Copy link

thank you!!!

@carloskim123
Copy link

Appreciate it man!

@Arunarikrishnan
Copy link

else {
alert("You didn't enter rock, paper, or scissors.");
location.reload();
}

@Eddiexxx
Copy link

Eddiexxx commented Oct 2, 2023

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