-
-
Save codecademydev/87c1a1c60b84aba14a140a15ef6075c1 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(); | |
return (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') || userInput === 'bomb' ? userInput : console.log('Invalid choice!'); | |
} | |
const getComputerChoice = () => { | |
const seed = Math.floor(Math.random() * 3); | |
switch (seed) { | |
case 0: | |
return 'rock'; | |
case 1: | |
return 'paper'; | |
case 2: | |
return 'scissors'; | |
} | |
} | |
const determineWinner = (userChoice, computerChoice) => { | |
if (userChoice === computerChoice) { | |
return "It's a tie!"; | |
} | |
if (userChoice === 'bomb') { | |
return "You won!"; | |
} | |
if (userChoice === 'rock') { | |
if (computerChoice === 'paper') { | |
return "Computer wins!"; | |
} else { | |
return "You won!"; | |
} | |
} | |
if (userChoice === 'paper') { | |
if (computerChoice === 'scissors') { | |
return "Computer wins!"; | |
} else { | |
return "You won!"; | |
} | |
} | |
if (userChoice === 'scissors') { | |
if (computerChoice === 'rock') { | |
return "Computer wins!"; | |
} else { | |
return "You won!"; | |
} | |
} | |
} | |
const playGame = () => { | |
const userChoice = getUserChoice('bomb'); | |
const computerChoice = getComputerChoice(); | |
if (userChoice === undefined) { | |
return 'Please pick a valid choice.'; | |
} | |
console.log(`You play ${userChoice}. Computer plays ${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