mardi 12 mai 2020

Need help in my rock paper and scissors game javascript

When the user and computer tied, it keeps telling me that I LOST instead of being tied.

<html>

<head>

    <title>Rock, Paper and Scissors</title>

</head>

<body>

    <p>Rock, Paper and Scissors!</p>

    <input type="text" id ="textChoice">

    <button id="submit">Submit</button>

    <p id="paragraphChoice"></p>

    <script type="text/javascript">

        document.getElementById("submit").onclick = function() {

            var computerChoice = ["rock", "paper", "scissors"];

            var computerGuess = computerChoice[Math.floor(Math.random() * computerChoice.length)];

            var userGuess = document.getElementById("textChoice").value;

            if (userGuess === computerGuess) {

                document.getElementById("paragraphChoice").innerHTML = "You tied! The computer choose "+ computerGuess; //Both tied!

            }

            if (userGuess === "rock") {

                if (computerGuess === "scissors") {
                    //rock wins
                    document.getElementById("paragraphChoice").innerHTML = "You won! The computer choose "+ computerGuess;

                } else {
                    //paper wins
                    document.getElementById("paragraphChoice").innerHTML = "You lost! The computer choose "+ computerGuess;

                }

            }

            if (userGuess === "paper") {

                if (computerGuess === "rock") {
                    //rock wins
                    document.getElementById("paragraphChoice").innerHTML = "You won! The computer choose "+ computerGuess;

                } else {
                    //scissors wins
                    document.getElementById("paragraphChoice").innerHTML = "You lost! The computer choose "+ computerGuess;

                }

            }

            if (userGuess === "scissors") {

                if (computerGuess === "paper") {

                    document.getElementById("paragraphChoice").innerHTML = "You won! The computer choose "+ computerGuess;

                } else {

                    document.getElementById("paragraphChoice").innerHTML = "You lost! The computer choose "+ computerGuess;

                }

            }

        };

    </script>

</body>

I haven't learned functions so far, the if statements are pretty messy(well that's what I think).

Thank you very much for your help!

Aucun commentaire:

Enregistrer un commentaire