Advertisement
Guest User

kamienpapiernozyce

a guest
Feb 21st, 2018
72
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'){
  4.     return userInput;
  5.   } else{
  6.      console.log('error');
  7.      }
  8.      
  9. };
  10.  
  11. const getComputerChoice= () => {
  12.  let randomNumber = Math.floor(Math.random()*3);
  13.  
  14.   switch (randomNumber){
  15.     case 0:
  16.       return 'rock';
  17.       break;
  18.     case 1:
  19.       return 'paper';
  20.         break;
  21.     case 2:
  22.       return 'scissors'}
  23. };
  24.  
  25. const determineWinner = (userChoice, computerChoice) => {
  26.   if(userChoice === computerChoice){
  27.     return 'tie';
  28.   }
  29.  
  30.  
  31.   if(userChoice === 'rock'){
  32.     if(computerChoice === 'paper'){
  33.       return 'Pc won';
  34.     }else{
  35.       return 'You won';
  36.     }
  37.   }
  38.  
  39.   if(userChoice === 'paper'){
  40.     if(computerChoice === 'scissors'){
  41.       return 'Pc won';
  42.     }else{
  43.       return 'You won';
  44.     }
  45.   }
  46.  
  47.   if(userChoice === 'scissors'){
  48.     if(computerChoice === 'rock'){
  49.       return 'Pc won';
  50.     }else{
  51.       return 'You won';
  52.     }
  53.   }
  54.  
  55. };
  56.  
  57. const playGame = () =>{
  58.   const userChoice = getUserChoice('rock');
  59.     const computerChoice = getComputerChoice();
  60.   console.log('You threw: ' + userChoice);
  61.     console.log('Pc threw: ' + computerChoice);
  62.   console.log(determineWinner(userChoice, computerChoice));
  63. }
  64. playGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement