Skip to content

Instantly share code, notes, and snippets.

@Higokian
Last active May 31, 2020 17:37
Show Gist options
  • Save Higokian/a7460fc7d884d40b9b201115637516a2 to your computer and use it in GitHub Desktop.
Save Higokian/a7460fc7d884d40b9b201115637516a2 to your computer and use it in GitHub Desktop.
// This is a simple rock, paper, scissors game.
// Figure out the secret cheat code to win everytime!
// Author: Jason Ritter
// Last updated: 05/30/2020
// Start date: 05/30/2020
// Get the users choice
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
return userInput;
} else {
console.log('Please type rock, paper, or scissors');
}
};
// Get the computers choice
const getComputerChoice = () => {
let randNum = Math.floor(Math.random() * 3);
switch(randNum) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
};
// Determine the winner
const determineWinner = (userChoice, computerChoice) => {
// If both choices are the same, it's a tie
if(userChoice === computerChoice) {
return console.log("It's a tie!");
}
// If user chooses 'bomb', they win
if(userChoice === 'bomb') {
return console.log("The user wins!");
}
// Determine winner if user chooses rock
if(userChoice === 'rock') {
if(computerChoice === 'paper') {
return console.log('The computer wins!');
} else {
return console.log('The user wins!');
}
}
// Determine winner if user chooses paper
if(userChoice === 'paper') {
if(computerChoice === 'scissors') {
return console.log('The computer wins!');
} else if(computerChoice === 'rock') {
return console.log('The user wins!');
}
}
// Determine winner if user chooses scissors
if(userChoice === 'scissors') {
if(computerChoice === 'rock') {
return console.log('The computer wins!');
} else if(computerChoice === 'paper');
return console.log('The user wins!');
}
};
// Play the game to see who wins!
const playGame = () => {
const userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('The user threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
};
// Play the game
playGame();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment