Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 27, 2020 19:27
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/07b064a81ad7897ab576f4dae2a1234c to your computer and use it in GitHub Desktop.
Save codecademydev/07b064a81ad7897ab576f4dae2a1234c 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 = () => {
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 === 'paper' && computerChoice === 'rock' || userChoice === 'scissors' && computerChoice === 'paper' || userChoice === 'rock' && computerChoice === 'scissors' || userChoice === 'bomb'){
console.log("You win! :D")
};
if (userChoice === 'rock' && computerChoice === 'paper' || userChoice === 'paper' && computerChoice === 'scissors' || userChoice === 'scissors' && computerChoice === 'rock') {
console.log("The computer wins. Game Over. D:");
};
};
const playGame = () => {
// you can fill in your choice by replacing 'rock'!
const userChoice = getUserChoice('rock');
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