Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created March 16, 2022 05:42
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/b9bf9b73cf3fbbc58eaec078a62e77eb to your computer and use it in GitHub Desktop.
Save codecademydev/b9bf9b73cf3fbbc58eaec078a62e77eb 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, Please type in rock, paper, or scissors.')
}
}
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 4);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
};
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'This game is a tie!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'Paper beats rock! You lose!';
} else {
return 'Rock beats scissors! You win!';
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'Scissors beat paper! You lose!';
} else {
return 'Paper beats rock! You win!';
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'Rock beats scissors! You lose!';
} else {
return 'Scissors beats paper! You win!';
}
}
if (userChoice === 'bomb') {
if (computerChoice === 'paper') {
return 'BOOM! You win!';
} else if (computerChoice === 'rock') {
return 'BOo0oM! You win!';
} else {
return 'BOOOOM! You win';
}
}
};
const playGame = () => {
const userChoice = getUserChoice('bomb');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
console.log(playGame())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment