Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.