Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created November 21, 2020 10: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/dd28e2427343294d88fa94fba5d7dde7 to your computer and use it in GitHub Desktop.
Save codecademydev/dd28e2427343294d88fa94fba5d7dde7 to your computer and use it in GitHub Desktop.
Codecademy export
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {console.log(userInput)
} else {
console.log('Error. Please type rock, paper or scissors.')
}
};
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 === 'rock') {
if (computerChoice === 'scissors') {
return 'You are the winner!';
} else {
return 'The computer won!';
} if (userChoice === 'paper') {
if (computerChoice === 'rock') {
return 'You lost.';
} else {
return 'You are the winner!';
} if (userChoice === 'scissors') {
if (computerChoice === 'paper') {
return 'You won against the computer!';
} else {
return 'You lost.';
}
}
}
}
}
}
};
playGame() => {
const userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('You threw:' + userChoice);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment