Skip to content

Instantly share code, notes, and snippets.

@Kharouk
Last active June 18, 2018 09:41
Show Gist options
  • Save Kharouk/8b3cbff4307bb3fc79521426391ce9f7 to your computer and use it in GitHub Desktop.
Save Kharouk/8b3cbff4307bb3fc79521426391ce9f7 to your computer and use it in GitHub Desktop.
My Rock Paper Scissors Variation
function getComputerChoice() {
randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
break;
}
}
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "slap") {
return userInput;
} else {
console.log("Guess what, you managed to break the game. Hope you feel good.");
}
};
function determineWinner(userChoice, computerChoice) {
if (userChoice === "slap") {
return "I can't believe you've just done that. You win!";
}
if (userChoice === computerChoice) {
return "The game is a tie.";
}
if (userChoice === "rock") {
if (computerChoice === "paper") {
return "The computer won. Sorry!";
} else {
return "You win! Congrats!";
}
}
if (userChoice === "paper") {
if (computerChoice === "scissors") {
return "The computer won. Sorry!";
} else {
return "You win! Congrats!";
}
}
if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "The computer won. Sorry!";
}
else {
return "You win! Congrats!";
}
}
}
const playGame = () => {
const userChoice = getUserChoice("slap");
const computerChoice = getComputerChoice();
console.log(`You threw: ${userChoice}`);
console.log(`The 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