vendredi 23 octobre 2015

If then statement for trivia

I'm just starting out and stuck on what's seemingly a basic If then statement. Hoping some could give me their thoughts.

I'm trying to design a trivia question where the user has to guess an election year. If they get it right they should receive a "You're right!" alert; when wrong a "You're wrong" alert. However, it seems like no matter the answer being submitted, it always returns a "You're right" alert.

This has been wracking my brain for the past three hours, so any feedback is much appreciated. Thank you!!!

<!doctype html>
<html>  <title>CodePlayer</title>


<head>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <script src="http://ift.tt/1dLCJcb"></script>

    <script src="http://ift.tt/1LrXp9A"></script>


    <style>

    .container {
        margin: 50px 0 0 50px;
    }



    </style>


</head>


<body>

    <div class="container">

        When was Obama elected President?
        <input id="answer" />

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


    </div>


    <script>


        var electionGuess = document.getElementById("answer").value;

        var electionYear = 2008;


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

            if (electionGuess = electionYear) {

                alert("You're right!");

            } else if (electionGuess != electionYear) {

                alert("No you're wrong")
            }
        }

    </script>




</body>

</html>

Aucun commentaire:

Enregistrer un commentaire