Let's Play "Rock", "Paper", or "Scissors" With JavaScript

Let's Play "Rock", "Paper", or "Scissors" With 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.

SUBSCRIBE TO OUR NEWSLETTER

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").

SUBSCRIBE TO OUR NEWSLETTER

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

要查看或添加评论,请登录

Arun Rajendran的更多文章

  • 10 Powerful LinkedIn Tips To Build An Incredible Profile

    10 Powerful LinkedIn Tips To Build An Incredible Profile

    The professional social networking site LinkedIn was created with business and career-related goals in mind. It offers…

  • Benefits of Front-End Developer

    Benefits of Front-End Developer

    Front-end developers play a crucial role in web development for several reasons: User Experience: Front-end developers…

  • Do you Wanna Become a Versatile Full-Stack Developer in 2023?

    Do you Wanna Become a Versatile Full-Stack Developer in 2023?

    As we move further into 2023, the demand for full-stack developers continues to grow. Being a full-stack developer…

  • Guess Game Using JavaScript Number Methods

    Guess Game Using JavaScript Number Methods

    Hey, Connections, I would like to start my journey towards excellence in MERN STACK Development, Starting off learning…

    2 条评论
  • Best Chrome Extensions For Digital marketing In 2022

    Best Chrome Extensions For Digital marketing In 2022

    The Needs and Services of digital marketing continue to be in an increased state and more content at Digital marketers…

    1 条评论
  • HOW TO BECOME AN EFFECTIVE CONTENT WRITER IN 2022

    HOW TO BECOME AN EFFECTIVE CONTENT WRITER IN 2022

    WHO IS A CONTENT WRITER: Content writing is the process of planning, creating, and publishing content for digital…

  • Best digital marketing company in Coimbatore

    Best digital marketing company in Coimbatore

    Are you looking for the Best digital marketing company in Coimbatore, If so, you’ve come to the right place? I have…

    2 条评论
  • Billion Dollar Design - Canva

    Billion Dollar Design - Canva

    In this Article am gonna say about the 2nd richest woman in Australia And she is none other than Melanie Perkins, CEO…

  • JOIN THE REVOLUTION

    JOIN THE REVOLUTION

    Hey people, In this article, I am going to talk about, The very first Ola Electric S1 scooter that has already rolled…

  • LEARN THE MARKETING LAW WHICH MAKES YOU A MILLIONAIRE??

    LEARN THE MARKETING LAW WHICH MAKES YOU A MILLIONAIRE??

    In this article, I am going to talk about The Fundamentals Of Marketing, Digital Marketing Vs Traditional Marketing…

    8 条评论

社区洞察

其他会员也浏览了