Advertisement
chrispicy

Rock, Paper, Scissors.js

Aug 19th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const getUserChoice = userInput =>{
  2.   userInput = userInput.toLowerCase()
  3.   if (userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "bomb") {
  4.     return userInput;
  5.   } else {
  6.     console.log("An error has occurred.");
  7.   }
  8. };
  9.  
  10. const getComputerChoice = () =>{
  11.   switch (Math.floor(Math.random() * 2.99999)){
  12.     case 0:
  13.       return "rock"
  14.       break;
  15.     case 1:
  16.       return "paper"
  17.       break;
  18.     case 2:
  19.       return "scissors"
  20.       break;
  21.     default:
  22.       return null
  23.   }
  24. }
  25.  
  26. const determineWinner = (userChoice, computerChoice) => {
  27.   if (userChoice === computerChoice){
  28.     return "The game is a tie.";
  29.   } else if (userChoice === 'rock') {
  30.     if (computerChoice === 'paper') {
  31.       return "Computer wins.";
  32.     } else {
  33.       return "Player wins."
  34.     }
  35.   } else if (userChoice === 'paper') {
  36.     if (computerChoice === 'scissors') {
  37.       return "Computer wins.";
  38.     } else {
  39.       return "Player wins.";
  40.     }
  41.   } else if (userChoice === 'scissors') {
  42.     if (computerChoice === 'rock') {
  43.       return "Computer wins.";
  44.     } else {
  45.       return "Player wins.";
  46.     }
  47.   } else if (userChoice === 'bomb') {
  48.     return "Player activated cheat codes; Player wins"
  49.   }
  50. }
  51.  
  52. const playGame = () => {
  53.   let userChoice = getUserChoice('ROCK');
  54.   let computerChoice = getComputerChoice();
  55.   console.log(`You threw ${userChoice}.`)
  56.   console.log(`Computer threw ${computerChoice}.`)
  57.   console.log(determineWinner(userChoice, computerChoice))
  58. }
  59.  
  60. playGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement