Advertisement
Guest User

Untitled

a guest
May 29th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var userChoice = "";
  2. var computerChoice = "";
  3. var translate = 0;
  4. var noWinner = true;
  5.  
  6.  
  7. function verifyChoice(a){
  8.       if((a === "rock") || (a ===  "paper") || (a === "scissors")){
  9.         return 0;
  10.       }
  11.       else {
  12.         return 1;
  13.       }
  14. };
  15.  
  16.  
  17. function computerRandom(){
  18.   translate = Math.random();
  19.   if(translate < 0.34){
  20.     return "rock";
  21.   }
  22.   else if(translate <= 0.67){
  23.     return "paper";
  24.   }
  25.   else{
  26.     return "scissors";
  27.   }
  28. };
  29.  
  30. //returns true if choice wins
  31. function compare(choice, computerChoice){
  32.     if ((choice === "rock"      && computerChoice === "scissors")||
  33.         (choice === "paper"     && computerChoice === "rock")||
  34.         (choice === "scissors"  && computerChoice === "paper")){
  35.             return 1;
  36.         } else {
  37.             return 0;
  38.         }
  39. };
  40.  
  41.  
  42. do{
  43.     userChoice = prompt("Do you choose rock, paper or scissors?");
  44.     if(verifyChoice(userChoice)){
  45.         computerChoice = computerRandom();
  46.         confirm("Computer chose: " + computerChoice);
  47.         if(computerChoice === userChoice){
  48.             confirm("It's a tie! Try again!");
  49.         }else if(compare(userChoice,computerChoice)){
  50.             confirm("You win!");
  51.             noWinner = false;
  52.         }else{
  53.             confirm("Computer wins, you suck!");
  54.             noWinner = false;
  55.         }
  56.     }else{
  57.         confirm("That is not a valid option");
  58.     }
  59. }while(noWinner);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement