Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created June 30, 2020 17:52
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/d04bee496d77190ade9855343d1a3e62 to your computer and use it in GitHub Desktop.
Save codecademydev/d04bee496d77190ade9855343d1a3e62 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! Please try writing: rock, paper or scissors');
}
}
function getComputerChoice() {
let randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
}
}
function determineWinner (userChoice,computerChoice) {
if (userChoice === computerChoice) {
console.log("It's a tie!");
} else {
if (userChoice === 'rock') {
{
if (computerChoice === 'paper') {
return console.log('Computer won!');
} else {
return console.log('User won!');
}
}
}
if (userChoice === 'paper') {
{if (computerChoice === 'scissors') {
return console.log('Computer won!');
} else {
return console.log('User won!');
}
}
}
if (userChoice === 'scissors') {
{if (computerChoice === 'rock') {
return console.log('Computer won!');
} else {
return console.log('User won!');
}
}
}
}
}
function playGame () {
const userChoice = getUserChoice('paper');
console.log('You threw '+ userChoice);
const computerChoice = getComputerChoice();
console.log('Computer threw ' + 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