-
-
Save jlbruno/6367190 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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