Rock Paper Scissors Javascript

Every developer should be familiar with JavaScript because it is one of the three scripting languages used to create websites. JavaScript is one of the most essential programming languages, and it is widely used in the tech industry. JavaScript allows you to add functionality and behaviours to your website, whereas HTML and CSS give a website structure and style. This enables your website’s visitors to interact with content creatively.


Most of JavaScript’s functionality is client-side, meaning it runs in your browser on your computer. But more recently, Node.js’ release has also made it possible for JavaScript to run programs on servers. Since its introduction, JavaScript has surpassed other languages like Flash and Java because it is relatively simple to learn, has a free and open community, and—most importantly—is incredibly useful for enabling developers to quickly produce apps with millions of users. Here we will learn about Rock paper scissors with an example code in detail

function playGame(playerChoice) // Generate a random choice for the computer const choices = [“rock”, “paper”, “scissors”]; const computerChoice = choices[Math.floor(Math.random() * choices.length)]; // Compare the choices and determine the winner if (playerChoice === computerChoice) { return “It’s a tie!”; } else if ( (playerChoice === “rock” && computerChoice === “scissors”) || (playerChoice === “paper” && computerChoice === “rock”) || (playerChoice === “scissors” && computerChoice === “paper”) ) { return “You win!”; } else { return “Computer wins!”; } } // Example usage const playerChoice = prompt(“Enter your choice (rock, paper, or scissors):”)//.toLowerCase(); const result = playGame(playerChoice); console.log(result);

The playGame function takes a parameter called playerChoice, which represents the choice made by the player (either “rock”, “paper”, or “scissors”).


Inside the function, an array called choices is defined, which contains the possible choices for the computer: “rock”, “paper”, and “scissors”.

The computer’s choice is randomly generated using Math.random() and Math.floor() functions. It generates a random index between 0 and the length of the choices array (exclusive) and selects the corresponding choice from the array.

The function then compares the player’s choice with the computer’s choice to determine the winner. If the player’s choice is the same as the computer’s choice, it means it’s a tie, and the function returns the string “It’s a tie!”.

If the player’s choice beats the computer’s choice according to the rules of rock-paper-scissors (rock beats scissors, paper beats rock, scissors beat paper), the function returns the string “You win!”.

If none of the above conditions is met, it means the computer’s choice beats the player’s choice, and the function returns the string “Computer wins!”.

Outside the function, there is an example of usage. The code prompts the player to enter their choice using the prompt() function and stores it in the playerChoice variable. The player’s choice is then passed as an argument to the playGame function, and the result is stored in the result variable.

Finally, the result is logged to the console using console.log(result). Overall, this code allows a player to play a game of rock-paper-scissors against the computer and displays the result.

To know more informative content which helps you to upskill yourself SUBSCRIBE TO OUR NEWSLETTER