-
-
Save studstill/e40775f0620c2472b9ca to your computer and use it in GitHub Desktop.
Rock, Paper, Scissors
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
<script> | |
var userChoice = prompt("Do you choose rock, paper or scissors?"); | |
var computerChoice = Math.random(); | |
if (computerChoice < 0.34) { | |
computerChoice = "rock"; | |
} else if(computerChoice <= 0.67) { | |
computerChoice = "paper"; | |
} else { | |
computerChoice = "scissors"; | |
} console.log("Computer: " + computerChoice); | |
var compare = function (choice1, choice2) { | |
if (choice1 === choice2) { | |
return "The result is a tie!" | |
/* Here is where I'm very confused: | |
The assignment was to have the game allow the user and computer to pick again, | |
but I cannot, for the life of me, figure our how to do it. At this point in the tutorials, | |
we have only used if/else if/else statements. | |
I tried: | |
return choice1 = prompt ("It's a tie guess again!") | |
But that just created an infinite loop. Should I create another function within | |
a function, or is there a way to just tell the original function to run again? | |
*/ | |
} | |
} | |
else if (choice1 === "rock") { | |
if (choice2 === "scissors") { | |
return "rock wins"; | |
} else { | |
return "scissors wins"; | |
} | |
} | |
else if (choice1 === "paper") { | |
if (choice2 === "rock") { | |
return "paper wins"; | |
} else { | |
return "rock wins"; | |
} | |
} | |
else if (choice1 === "scissors") { | |
if (choice2 === "rock") { | |
return "rock wins"; | |
} else { | |
return "scissors"; | |
} | |
} | |
} | |
compare(userChoice, computerChoice); | |
compare(userChoice, computerChoice); | |
alert(compare(userChoice,computerChoice)); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment