Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created July 19, 2018 13:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save codecademydev/13736318cf968ced5ee8b9979293a0ce 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!');
}
}
const getComputerChoice = randomNumber => { randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
function determineWinner (userChoice, computerChoice) {
if (userChoice === computerChoice) {
return 'game was a tie!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'computer won!';
} else {
return 'user won';
}
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'computer won!';
} else {
return 'user won';
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'computer won';
} else {
return 'user won';
}
}
const playGame = () => {
const userChoice =
getUserChoice('rock')
const computerChoice = getComputerChoice();
console.log(`you threw: ${userChoice}`);
console.log(`the computer threw: ${computerChoice}`);
console.log(determineWinner(userChoice, computerChoice));
}
console.log(getComputerChoice());
playGame();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment