Advertisement
makispaiktis

Codecademy - 10th Exercise (Rock, paper, scissors)

Oct 2nd, 2019 (edited)
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Create my function using an ARROW FUNCTION
  2. // It is not necessary to write "= (userInput) =>"
  3. // because I have only 1 parameter
  4.  
  5. const getUserChoice = userInput => {
  6.   // Lowercase option: User can type "rock" or "Rock" or sth else
  7.   const initialInput = userInput;
  8.   userInput = userInput.toLowerCase();
  9.   // Check if we have a valid string typed in
  10.   if(userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "bomb"){
  11.     return userInput;  
  12.   }
  13.   else{
  14.     console.log("Invalid option. You typed in '" + initialInput + "'");
  15.   }
  16.  
  17. };
  18.  
  19.  
  20. // Classic way for function declaring
  21. function getComputerChoice(){
  22.   let choice = Math.floor(Math.random() * 3);
  23.   switch(choice){
  24.     case 0:
  25.       return "rock";
  26.       break;
  27.     case 1:
  28.       return "paper";
  29.       break;
  30.     case 2:
  31.       return "scissors";
  32.       break;
  33.     default:
  34.       console.log("Invalid option.");
  35.       break;
  36.   }
  37. }
  38.  
  39. // Function, that determines the winner
  40. function determineWinner(userChoice, computerChoice){
  41.  
  42.   // Firstly, I will check if someone has found
  43.   // the secret cheat code = "bomb"
  44.   if(userChoice === "bomb"){
  45.     console.log("User wins! He found the secret key-word to win!");
  46.   }
  47.  
  48.   // If the game is a tie (3 tie-combinations out  of the possible 9 ways)
  49.   if(userChoice === computerChoice){
  50.     console.log("It's a tie! Both sides chose: '" + userChoice + "'");
  51.   }
  52.              
  53.   // 6 different combinations of NOT THE SAME CHOICE by user and computer
  54.   else{  
  55.     // 1st combination of not a tie-game          
  56.     if(userChoice === "rock" && computerChoice === "scissors"){
  57.       console.log("User wins")
  58.     }
  59.     // 2nd combination
  60.     else if(userChoice === "rock" && computerChoice === "paper"){
  61.       console.log("Computer wins");
  62.     }
  63.     // 3rd combination
  64.     else if(userChoice === "scissors" && computerChoice === "paper"){
  65.       console.log("User wins");
  66.     }
  67.     // 4th combination
  68.     else if(userChoice === "scissors" && computerChoice === "rock"){
  69.       console.log("Computer wins");
  70.     }
  71.     // 5th combination
  72.     else if(userChoice === "paper" && computerChoice === "rock"){
  73.       console.log("User wins");
  74.     }
  75.     // 6th combination
  76.     else if(userChoice === "paper" && computerChoice === "scissors"){
  77.       console.log("Computer wins");
  78.     }
  79.    
  80.   } // End of else-case
  81. }
  82.  
  83.  
  84. function playGame(){
  85.   const userChoice = getUserChoice("rock");
  86.   const computerChoice = getComputerChoice();
  87.   console.log("  User chooses  : " + userChoice);
  88.   console.log("Computer chooses: " + computerChoice);
  89.   console.log();
  90.   determineWinner(userChoice, computerChoice);
  91. }
  92.  
  93. // Main part of the game
  94. playGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement