Skip to content

Instantly share code, notes, and snippets.

@Merrit
Last active July 8, 2018 22:58
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 Merrit/d980c04a89e84ff110698aa1100dce87 to your computer and use it in GitHub Desktop.
Save Merrit/d980c04a89e84ff110698aa1100dce87 to your computer and use it in GitHub Desktop.
// Rock, Paper, Scissors - Challenge
let userChoice = 'Rock';
function computerChoice() {
let number = Math.round((Math.random() * 3) + 1);
if (number < 1) {
computerChoice();
}
if (number === 1) {
return 'Rock';
} else if (number === 2) {
return 'Paper';
} else if (number === 3) {
return 'Scissors';
} else {
return 'There was an unknown error.';
}
}
function checkWinner(userChoice, computerChoice) {
if (userChoice == 'Rock' && computerChoice == 'Rock') {
console.log(`User: ${userChoice}, Computer: ${computerChoice}.`);
console.log('Tie!');
return 'Tie!';
} else if (userChoice == 'Rock' && computerChoice == 'Paper') {
console.log(`User: ${userChoice}, Computer: ${computerChoice}.`);
console.log('Computer wins!');
return 'Computer wins!';
} else if (userChoice == 'Rock' && computerChoice == 'Scissors') {
console.log(`User: ${userChoice}, Computer: ${computerChoice}.`);
console.log('User wins!');
return 'User wins!';
} else if (userChoice == 'Paper' && computerChoice == 'Rock') {
console.log(`User: ${userChoice}, Computer: ${computerChoice}.`);
console.log('User wins!');
return 'User wins!';
} else if (userChoice == 'Paper' && computerChoice == 'Paper') {
console.log(`User: ${userChoice}, Computer: ${computerChoice}.`);
console.log('Tie!');
return 'Tie!';
} else if (userChoice == 'Paper' && computerChoice == 'Scissors') {
console.log(`User: ${userChoice}, Computer: ${computerChoice}.`);
console.log('Computer wins!');
return 'Computer wins!';
} else if (userChoice == 'Scissors' && computerChoice == 'Rock') {
console.log(`User: ${userChoice}, Computer: ${computerChoice}.`);
console.log('Computer wins!');
return 'Computer wins!';
} else if (userChoice == 'Scissors' && computerChoice == 'Paper') {
console.log(`User: ${userChoice}, Computer: ${computerChoice}.`);
console.log('User wins!');
return 'User wins!';
} else if (userChoice == 'Scissors' && computerChoice == 'Scissors') {
console.log(`User: ${userChoice}, Computer: ${computerChoice}.`);
console.log('Tie!');
return 'Tie!';
}
}
checkWinner(userChoice, computerChoice());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment