-
-
Save codecademydev/f578231890338ce4029d0057043fc752 to your computer and use it in GitHub Desktop.
Codecademy export
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
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