-
-
Save codecademydev/dd28e2427343294d88fa94fba5d7dde7 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
const getUserChoice = userInput => { | |
userInput = userInput.toLowerCase(); | |
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {console.log(userInput) | |
} else { | |
console.log('Error. Please type rock, paper or scissors.') | |
} | |
}; | |
const getComputerChoice = () => { | |
const randomNumber = Math.floor(Math.random() * 3); | |
switch(randomNumber) { | |
case 0: | |
return 'rock'; | |
case 1: | |
return 'paper'; | |
case 2: | |
return 'scissors'; | |
} | |
}; | |
const determineWinner(userChoice, computerChoice) => { | |
if (userChoice === computerChoice) { | |
return 'the game is a tie'; | |
} if (userChoice === 'rock') { | |
if (computerChoice === 'scissors') { | |
return 'You are the winner!'; | |
} else { | |
return 'The computer won!'; | |
} if (userChoice === 'paper') { | |
if (computerChoice === 'rock') { | |
return 'You lost.'; | |
} else { | |
return 'You are the winner!'; | |
} if (userChoice === 'scissors') { | |
if (computerChoice === 'paper') { | |
return 'You won against the computer!'; | |
} else { | |
return 'You lost.'; | |
} | |
} | |
} | |
} | |
} | |
} | |
}; | |
playGame() => { | |
const userChoice = getUserChoice('scissors'); | |
const computerChoice = getComputerChoice(); | |
console.log('You threw:' + userChoice); | |
console.log(determineWinner(userChoice, computerChoice)); | |
}; | |
playGame(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment