Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created June 8, 2020 15:51
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/a234d5cd225afc6414a305c4c2bd172e to your computer and use it in GitHub Desktop.
Save codecademydev/a234d5cd225afc6414a305c4c2bd172e to your computer and use it in GitHub Desktop.
Codecademy export
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if ((userInput === 'rock') || (userInput === 'paper') || (userInput === 'scissors')) {
return userInput;
} else {
console.log('Error, invalid term entered.');
}
};
const getComputerChoice = () => {
let randomNumber = Math.floor(Math.random() * 3);
if (randomNumber === 0) {
return 'rock';
} else if (randomNumber === 1) {
return 'paper';
} else if (randomNumber === 2) {
return 'scissors';
} else {
return 'error';
}
};
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'The game is a tie!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'You lose!';
} else {
return 'You win!';
} else if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'You win!';
} else {
return 'You lose!';
} else if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'You lose!';
} else {
return 'You win!';
}
} else {
return 'error';
}
};
console.log(determineWinner('rock', 'paper'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment