Skip to content

Instantly share code, notes, and snippets.

@debonx
Created July 27, 2019 16:54
Show Gist options
  • Save debonx/0b7ad01ab93a724a7e816adb296f4d4f to your computer and use it in GitHub Desktop.
Save debonx/0b7ad01ab93a724a7e816adb296f4d4f to your computer and use it in GitHub Desktop.
Rock, paper or scissors? A simple Javascript game.
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
return userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' ? userInput : console.log('Invalid input');
}
const getComputerChoice = () => {
let whole = Math.floor(Math.random() * 3);
switch( whole ) {
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
default:
return 'scissors';
break;
}
}
const determineWinner = (userChoice, computerChoice) => {
if(userChoice === computerChoice) {
return 'The game is a tie';
}
if(userChoice === 'bomb'){
return 'Everything exploded, user wins.';
}
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 = () => {
let userChoice = getUserChoice('rock');
console.log(userChoice);
let computerChoice = getComputerChoice();
console.log(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