[JavaScript] Simple Rock Paper Scissors Game - Beginner Friendly
Build a mini rock paper scissors game in 5 minutes!
![[JavaScript] Simple Rock Paper Scissors Game - Beginner Friendly](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1683298532208%2F43c7200a-33fd-411b-8e6f-7b8a4ea8d031.png&w=3840&q=75)
Computer Science Enthusiast with a Drive for Excellence | Data Science | Web Development | Passionate About Tech & Innovation
Introduction
To build a rock paper scissors game, you need to use Math.random() method to randomly generate rock, scissors and paper.
In this mini-build, we will use HTML to allow users to choose their option and print out the result if they won against the computer.
getRandomInt() Function
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) - min);
}
This function generates a random number between min and max values.
For example, getRandomInt(1, 10) will give a random number between 1 and 10.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<button onclick="rspGame('rock');">rock</button>
<button onclick="rspGame('paper');">paper</button>
<button onclick="rspGame('scissors');">scissors</button>
</head>
<body>
<script>
function rspGame(userInput) {
computerRsp = ["rock", "paper", "scissors"];
computerOutput = computerRsp[getRandomInt(0, 2)]
userWinValue = {
rock: "scissors",
paper: "rock",
scissors: "paper"
};
console.log("User Side:", userInput);
console.log("Computer Side:", computerOuput);
return userWinValue[userInput] === computerOutput
? "User Wins!"
: userInput = comptuerOutput
? "Draw!"
: "Computer Wins!";
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) - min);
}
</script>
</body>
</html>
The portion below lets the user makes their choice.
If a click on the button is detected, the computer generates its output (one of rock, paper, scissors), then prints out the results.
<button onclick="rspGame('rock');">rock</button>
<button onclick="rspGame('paper');">paper</button>
<button onclick="rspGame('scissors');">scissors</button>
![[CS fundamentals] Linked List Creation & Insertion explained in C.](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F644c253f17f6efe1e02ade41%2F6a74e5c0-44ca-4110-9ee7-e8e67767a9dc.png&w=3840&q=75)
![[CS Fundamentals] Pointers in C](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F644c253f17f6efe1e02ade41%2F6f81f5a6-d87c-4382-883f-fa4d0a8b5968.png&w=3840&q=75)
![[SQL] Writing Efficient SQL Queries](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1751292603168%2Fb0b43c7d-8e08-47b2-a64c-b600e3511831.jpeg&w=3840&q=75)
![[CS Fundamentals] Binary Search](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1747041878054%2Fc8d8adb9-097d-484b-9562-f43e4bce9a46.png&w=3840&q=75)
![[CS Fundamentals] Sorting Algorithms](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1746973590876%2Ffab37371-1496-42ef-950b-432e7637c89e.png&w=3840&q=75)