vendredi 29 avril 2016

How to count the number of correct answers using Javascript?

I'm trying to create a simple website about counting the number of fingers I'm putting up (random number). However, I want to add something extra to the website. I'm trying to create a counter on the side of the container that counts the number of correct answers, and drops down to zero whenever the user get a question incorrectly. Here is my code for the site...

    <!doctype html>
<html>
    <head>  
        <title>Changing Website Content</title>

        <link href='http://ift.tt/OIiP2w' rel='stylesheet' type='text/css'>


        <style type="text/css">

            body {

            background-image: url(nyc.jpg);
            width:100%;
            font-family: 'Open Sans', sans-serif;


            }

            #main {

                margin: 100px;
                background-color: #ede1e1;
                padding: 50px;
                position: relative;
                top: 50px;

            }



        </style>



    </head>

    <body>




        <div id="main">

            <p>How many fingers am I holding up? Pick a number 1 - 5.</p>


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


            <button type="submit" id="fingerChecker">Guess?</button>



        </div>









        <script type="text/javascript">


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

                var fingers = document.getElementById("fingers").value;

                var random1 = (Math.floor((Math.random() * 5) + 1))

                if (fingers == (random1)) {

                    alert("Yes, you are correct!")

                } else {

                    alert("Nope! I had " + random1);
                }

           }


        </script>


    </body>
</html>

Any help is welcomed. (This is my fist post so excuse any mistakes I may have made!)




Aucun commentaire:

Enregistrer un commentaire