DEV Community

Cover image for 🟨 JavaScript 5 | 🪨 Rock 📄 Paper  ✂️ Scissors with functions()
Gregor Schafroth
Gregor Schafroth

Posted on

🟨 JavaScript 5 | 🪨 Rock 📄 Paper  ✂️ Scissors with functions()

Instead of getting a ChatGPT exercise, today I just continued with the JavaScript course by SuperSimpleDev learned about functions in JavaScript. We upgraded the Rock Paper Scissors game code from last time by using functions, making the code much shorter.

Since I know functions from python there was not much of a surprise for me here, thats fortunately almost all the same 🙂

My Code

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rock Paper Scissors</title>
</head>

<body>
    <p>Rock Paper Scissors</p>
    <button onclick="
        playGame('rock')
">Rock</button>
    <button onclick="
        playGame('paper');
">Paper</button>
    <button onclick="
        playGame('scissors');
">Scissors</button>
    <script>
        function playGame(playerMove) {
            const computerMove = pickComputerMove();

            let result = '';

            if (playerMove === 'scissors') {
                if (computerMove === 'rock') {
                    result = 'You lose.';
                } else if (computerMove === 'paper') {
                    result = 'You win';
                } else if (computerMove === 'scissors') {
                    result = 'Tie';
                }

            } else if (playerMove === 'paper') {
                if (computerMove === 'rock') {
                    result = 'You win.';
                } else if (computerMove === 'paper') {
                    result = 'Tie';
                } else if (computerMove === 'scissors') {
                    result = 'You lose';
                }

            } else if (playerMove === 'rock') {
                if (computerMove === 'rock') {
                    result = 'Tie.';
                } else if (computerMove === 'paper') {
                    result = 'You Lose';
                } else if (computerMove === 'scissors') {
                    result = 'You Win';
                }
            }

            console.log(`You picked ${playerMove}. Computer picked ${computerMove}. ${result}`)

        }

        function pickComputerMove() {
            let computerMove = '';
            const randomNumber = Math.random();
            if (randomNumber >= 0 && randomNumber < 1 / 3) {
                computerMove = 'rock';
            } else if (randomNumber >= 1 / 3 && randomNumber < 2 / 3) {
                computerMove = 'paper';
            } else {
                computerMove = 'scissors';
            };

            return computerMove;
        }
    </script>
</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay