Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created March 30, 2017 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/f578231890338ce4029d0057043fc752 to your computer and use it in GitHub Desktop.
Save codecademydev/f578231890338ce4029d0057043fc752 to your computer and use it in GitHub Desktop.
Codecademy export
function GetUserChoice()
{
var userInput = prompt ('Rock', 'Paper', 'or Scissors?');
userInput =
userInput.toLowerCase();
if (
userInput==="rock" ||
userInput==="paper"||
userInput==="scissor")
{
return (userInput);
} else {
console.log ("Error!");
}
}
function getComputerChoice ()
{
Math.floor(Math.random() *3);
switch(randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';}
}
function determineWinner (userChoice, computerChoice) {
if (userChoice === computerChoice)
{return "DRAW!";}
//rock and paper
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!';
}
}
//paper and scissor
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'The computer won!';
} else {
return 'You won!';
}
}
// scissor and rock
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!';
}
}
function playGame() {
var userChoice = getUserChoice();
var computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGame();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment