Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created February 19, 2021 04:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/87c1a1c60b84aba14a140a15ef6075c1 to your computer and use it in GitHub Desktop.
Save codecademydev/87c1a1c60b84aba14a140a15ef6075c1 to your computer and use it in GitHub Desktop.
Codecademy export
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
return (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') || userInput === 'bomb' ? userInput : console.log('Invalid choice!');
}
const getComputerChoice = () => {
const seed = Math.floor(Math.random() * 3);
switch (seed) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return "It's a tie!";
}
if (userChoice === 'bomb') {
return "You won!";
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return "Computer wins!";
} else {
return "You won!";
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return "Computer wins!";
} else {
return "You won!";
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return "Computer wins!";
} else {
return "You won!";
}
}
}
const playGame = () => {
const userChoice = getUserChoice('bomb');
const computerChoice = getComputerChoice();
if (userChoice === undefined) {
return 'Please pick a valid choice.';
}
console.log(`You play ${userChoice}. Computer plays ${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