benjaminvr

Codecademy - Rock, paper, scissors

Jul 4th, 2021
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const getUserChoice = userInput => {
  2.   userInput.toLowerCase()
  3.     if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
  4.       return userInput;
  5.     } else {
  6.       console.log('Input error, choose rock, paper or scissors.');
  7.     }
  8. };
  9.  
  10. const getComputerChoice = () => {
  11.   let randomChoice = Math.floor(Math.random()*3);
  12.   switch(randomChoice){
  13.     case 0:
  14.       return 'rock';
  15.       break;
  16.     case 1:
  17.       return 'paper';
  18.       break;
  19.     case 2:
  20.       return 'scissors';
  21.       break;
  22.   }
  23. }
  24.  
  25. const determineWinner = (userChoice, computerChoice) => {
  26.   if(userChoice === computerChoice){
  27.     return 'The game was a tie!';
  28.   }
  29.  
  30.   if(userChoice === 'rock'){
  31.     if(computerChoice === 'paper'){
  32.       return 'The computer won with paper!';
  33.     } else {
  34.       return 'The user won with rock!';
  35.     }
  36.   }
  37.  
  38.   if(userChoice === 'paper'){
  39.     if(computerChoice === 'scissors'){
  40.       return 'The computer won with scissors!';
  41.     } else {
  42.       return 'The user won with paper!';
  43.     }
  44.   }
  45.  
  46.   if(userChoice === 'scissors'){
  47.     if(computerChoice === 'rock'){
  48.       return 'The computer won with rock!';
  49.     } else {
  50.       return 'The user won with scissors!';
  51.     }
  52.   }
  53. }
  54.  
  55. const playGame = (userChoice) => {
  56.   userChoice = getUserChoice(userChoice);
  57.   let computerChoice = getComputerChoice();
  58.   console.log(`User is playing with: ${userChoice}\nComputer is playing with: ${computerChoice}`);
  59.   console.log(determineWinner(userChoice, computerChoice));
  60. }
  61.  
  62. playGame('rock');
Advertisement
Add Comment
Please, Sign In to add comment