Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created January 14, 2019 18:08
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/2b0b4255d240f9dda21493e1e831a8ce to your computer and use it in GitHub Desktop.
Save codecademydev/2b0b4255d240f9dda21493e1e831a8ce to your computer and use it in GitHub Desktop.
Codecademy export
const userChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else if (userInput === 'bomb') {
return userInput;
} else {
console.log('There was an error.');
}
}
if (userInput === 'bomb') {
return userInput
}
}
const compChoice = () => {
const randomNumber = (Math.floor(Math.random() * 3 ));
switch (randomNumber) {
case 0 :
return 'rock';
break;
case 1 :
return 'paper';
break;
case 2 :
return 'scissors';
break;
}
}
const determineWinner = (getUserChoice, getCompChoice) => {
if (getUserChoice === 'bomb') {
return 'You won with the power of the bomb!'
}
if (getUserChoice === getCompChoice) {
return 'The game was a tie!';
}
if (getUserChoice === 'rock') {
if (getCompChoice === 'paper') {
return 'The AI won!';
} else {
return 'You won!';
}
}
if (getUserChoice === 'paper') {
if (getCompChoice === 'scissors') {
return 'The AI won!';
} else {
return 'You won!';
}
}
if (getUserChoice === 'scissors') {
if (getCompChoice === 'rock') {
return 'The AI won!';
} else {
return 'You won!';
}
}
}
const playGame = () => {
const getUserChoice = userChoice('bomb');
const getCompChoice = compChoice();
console.log('You picked: ' + getUserChoice);
console.log('The AI picked: ' + getCompChoice);
console.log(determineWinner(getUserChoice, getCompChoice));
}
playGame();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment