Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created November 22, 2020 21:30
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/1bfe696e20d00b1b54e88b0062811bc6 to your computer and use it in GitHub Desktop.
Save codecademydev/1bfe696e20d00b1b54e88b0062811bc6 to your computer and use it in GitHub Desktop.
Codecademy export
const getUserChoice = userInput => {
userInput = userInput.toLowerCase()
if(
userInput === 'rock' ||
userInput === 'paper' ||
userInput === 'scissors' ||
userInput === 'bomb'
){
return userInput
} else {
console.log('Error!')
}
};
const getComputerChoice = () => {
Math.floor(Math.random() * 4);
switch (Math.floor(Math.random() * 4)){
case 0:
return 'rock'
break
case 1:
return 'paper'
break
case 2:
return 'scissors'
break
case 3:
return 'bomb'
break
}
};
const determineWinner = (userChoice, computerChoice) => {
if(userChoice === computerChoice){
return 'The game is a tie!'
}
if(userChoice === 'scissors'){
if(computerChoice === 'rock'){
return 'The computer won!'
}
if(userChoice === 'bomb'){
return 'You won!'
}
} else {
return 'You won!'
}
};
const playGame = () => {
const userChoice = getUserChoice('BOMB');
const 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