Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created July 27, 2020 08:36
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/0d438544b73fa4a73b60b8436b4d3374 to your computer and use it in GitHub Desktop.
Save codecademydev/0d438544b73fa4a73b60b8436b4d3374 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 message. please try it later');
}
}
console.log(getUserChoice('paper'));//should print 'paper'
console.log(getUserChoice('12'))//should print 'error~'and undefined
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 'tie';
} else if(userChoice === 'rock' && computerChoice === 'paper'){
return 'computer won';
} else {
return 'user won';
}
if (userChoice === 'scissors' && computerChoice === 'rock'){
return 'computer won';
} else {
return 'user won';
}
if (userChoice === 'paper' && computerChoice === 'scissors'){
return 'computer won';
} else {
return 'user won';
}
};
console.log(determineWinner('scissors', 'rock'));
console.log(determineWinner('paper', 'scissors'));
console.log(determineWinner('rock', 'paper'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment