Rock Paper Scissors using JavaScript

Click on play button after going through the instructions:

  1. Rock
  2. Paper
  3. Scissor

click play

Type either

  1. For Rock
  2. For Paper
  3. For Scissor

Type either

Game is drawn if the user election matches with computers:

Game is drawn

Player won against Computer.

Player won against Computer

Computer won against player.

Computer won against player

Generate a Random Number for computer.

If random number between 1 and 4 Rock.

If random number between 5 and 7 Paper.

Else take as Scissor

  1. var computerselection = "";  
  2. var y = (Math.floor(Math.random() * 10) + 1);  
  3.   
  4. if (y >= 1 && y <= 4) {  
  5.     computerselection = "Rock";  
  6. }  
  7. else if (y >= 5 && y <= 7) {  
  8.     computerselection = "Paper";  
  9. }  
  10. else {  
  11.     computerselection = "Scissor";  
  12. }  
Used Bootstrap Alert and Prompt.

Simple if conditions to check who have won.

Here x is the User Selection from Prompt.
  1. if (x == values[computerselection])   
  2.         bootbox.alert("Game Draw--Better Luck Next Time");  
  3.       
  4.     ---------------------------------------------------------------------------------------  
  5.     else if (values[computerselection] == 1 && x == 2)   
  6.         bootbox.alert("Computer have won  Computer Selected " + computerselection);  
  7.       
  8.     --------------------------------------------------------------------------------------------  
  9.   
  10.    else if ((values[computerselection] == 1 && x == 3) || (values[computerselection] == 2 && x == 1) || (values[computerselection] == 3 && x == 2))   
  11.        bootbox.alert("Computer have won  Computer Selected " + computerselection);  
  12.       
  13.    ----------------------------------------------------------------------------------------------  
  14.   
  15.     else if ((values[computerselection] == 1 && x == 2) || (values[computerselection] == 2 && x == 3) || (values[computerselection] == 3 && x == 1))   
  16.         bootbox.alert("You won against computer -- Computer Selected  " + computerselection);  

Ebook Download
View all
Learn
View all