Javascript question

Posted by Craig on Stack Overflow See other posts from Stack Overflow or by Craig
Published on 2011-02-17T23:08:56Z Indexed on 2011/02/17 23:25 UTC
Read the original article Hit count: 170

Filed under:
|

I am supposed to make this simple program. It produces a multiplication problem, and when the user types the correct answer, it is supposed to produce another question. Instead it goes into an infinite loop and never stops, the answer field and the button go away. Also, I am supposed to make the comment about the users answer, one of 4 different sayings. Without using Arrays how would I do that?

My professor is no help, really getting aggravated as I have no where else to turn.

<html>
    <title>HW 9.27 and 9.28</title>
    <head>
        <script type="text/javascript">
            var number1;
            var number2;
            var answer3;
            var answer2;

            function problem() {
                number1 = Math.floor(1 + Math.random() * 9);
                number2 = Math.floor(1 + Math.random() * 9);
                document.writeln("How much is " + number1 + " times " + number2 + " ?");
                answer2 = (number1 * number2);
            }

            function answer1() {
                var statusDiv = document.getElementById("status");
                answer3 = document.getElementById("answer").value;

                if (answer3 != answer2) statusDiv.innerHTML = "No. Please try again";
                else if (answer3 == answer2) {
                    statusDiv.innerHTML = "Very good!";
                    problem();
                }
            }
            problem();

        </script>
    </head>
    <body>
        <form>
            <input id="answer" type="text" />
            <input type="button" value="Solve!" onclick="answer1()" />
            <div id ="status">Click the Solve button to Solve the problem</div>
        </form>
    </body>
</html>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about homework