Skip to content

Instantly share code, notes, and snippets.

@jlbruno
Forked from joehinkle/RPS.js
Last active September 13, 2022 00:11
Show Gist options
  • Save jlbruno/6367190 to your computer and use it in GitHub Desktop.
Save jlbruno/6367190 to your computer and use it in GitHub Desktop.
// Initializing Variables
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
// Validation for user input
//if ( userChoice != "rock" || "paper" || "scissors" ) {
// userChoice = prompt("You did not select rock, paper, or scissors. Please try again.");
//}
// Randomly selecting correct value for computerChoice
if ( computerChoice < 0.34 ) {
computerChoice = "rock";
} else if ( computerChoice <= 0.67 ) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
// Function that compares the two values
function compare( userChoice, computerChoice ) {
if ( userChoice === computerChoice ) {
return "The result is a tie!";
}
switch ( userChoice ) {
case "rock":
return (computerChoice === "scissors" ? "Rock" : "Paper") + " wins!";
break;
case "paper":
return (computerChoice === "rock" ? "Paper" : "Scissors") + " wins!";
break;
case "scissors":
return (computerChoice === "paper" ? "Scissors" : "Rock") + " wins!";
break;
}
}
compare(userChoice, computerChoice);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment