Advertisement
Guest User

Untitled

a guest
Dec 5th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const getUserChoice = (userInput) => {
  2.   userInput = userInput.toLowerCase();
  3.  
  4.   if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb'){
  5.     return userInput;
  6.   } else {
  7.     console.log('You didn\'t do anything');
  8.   }
  9.  
  10. };
  11.  
  12. function getComputerChoice(){
  13.   const randomNumber = Math.floor(Math.random() * 3);
  14.  
  15.   switch(randomNumber) {
  16.     case 0:
  17.       return 'rock';
  18.       break;
  19.     case 1:
  20.       return 'paper';
  21.       break;
  22.     case 2:
  23.       return 'scissors';
  24.       break;
  25.   }
  26. }
  27.  
  28. function determineWinner(userChoice, computerChoice){
  29.  
  30.   if(userChoice === computerChoice){
  31.     return 'tie';
  32.   }
  33.  
  34.   if(userChoice === 'rock'){
  35.     if (computerChoice === 'paper'){
  36.       return 'Computer won';
  37.     } else {
  38.       return 'User won';
  39.     }
  40.   }
  41.    
  42.  if(userChoice === 'paper') {
  43.    if (computerChoice === 'scissors'){
  44.      return 'User won';
  45.    } else if (computerChoice === 'rock'){
  46.      return 'Computer won';
  47.    }
  48.  }
  49.  
  50.    if(userChoice === 'scissors') {
  51.    if (computerChoice === 'rock'){
  52.      return 'User won';
  53.    } else if (computerChoice === 'paper'){
  54.      return 'Computer won';
  55.    }
  56.  }
  57.  
  58.   if(userChoice === 'bomb'){
  59.     return 'U DESTROYED BRO';
  60.   }
  61.  
  62.  }
  63.  
  64. const playGame = () => {
  65.   const userChoice = getUserChoice('bomb');
  66.   const computerChoice = getComputerChoice();
  67.   console.log('You threw: ' + userChoice);
  68.   console.log('The computer threw:' + computerChoice);
  69.   console.log(determineWinner(userChoice, computerChoice));
  70. };
  71.  
  72. playGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement