1

I have a simple Javascript Math program. It lets you answer equations and when it checks your answer it alerts whether you answered correctly or wrongly.

Here is the code for addition:

function randomAdd()
        {
            var x=document.getElementById("AddN1");
            x.value=Math.floor((Math.random()*12)+1);

            var y=document.getElementById("AddN2");
            y.value=Math.floor((Math.random()*12)+1);
        }

        function checkAdd()
        {
            var z=document.getElementById("AddA2");
            z.value= parseInt(document.getElementById("AddN1").value) + parseInt(document.getElementById("AddN2").value);


            if(parseInt(document.getElementById("AddA1").value)==z.value)
            {
                score=score+1;
                alert("Addition: Correct");
            }
            else
            {
                alert("Addition: Incorrect");
            }
        }
<form name="Addition">
        <table>
            <tr>
                <th>Addition</th> <th></th> <th></th> <th></th> <th></th> <th></th>
            </tr>
            <tr>
                <th>Number 1</th> <th></th> <th>Number 2</th> <th></th> <th>Type Your Answer</th> <th>Correct Answer</th>
            </tr>
            <tr>
                <td><input type="text" name="AddN1" id="AddN1"></td> <td>+</td> <td><input type="text" name="AddN2" id="AddN2"></td> <td>=</td> <td><input type="text" name="AddA1" id="AddA1"></td> <td><input type="text" name="AddA2" id="AddA2"></td>
            </tr>
            <tr>
                <td><input type="button" value="Get Numbers" onclick="randomAdd();"> <td></td> <td></td> <td></td> <td><input type="button" value="Check Answer" onclick="checkAdd();"> <th></th>
            </tr>
        </table>
</form>

The code was provided to me by my college. I would like to keep a simple counter that will count all the correct answers the user gets and also a reset button that would reset the counter back to zero?

Sorry if this sounds really basic but I have no experience with Javascript.

Thanks.

crmepham
  • 4,676
  • 19
  • 80
  • 155

2 Answers2

2

I haven't tested but I suspect that your code works by default almost but only problem is that you are not declaring your score variable in the global scope, that way the same variable can be accessed by other functions without further passing.

Adding var score = 0; to top of page should make it work and later use it without var like score++; to increase by one, score = 0; to reset it and so on.. Rest of the code I leave you as a puzzle, since its school work anyway - just wanted to en-light you about usage of global variables. :) However try to avoid creating lots of global variables because it is also known as bad practice, although its sometimes hard to live without them. Read more from global variables, bad practice?

Few debugging tips for your code:

var scoreElement=document.getElementById("score");

This line is executed before DOM is loaded, so it might be null.. Try to change lines:

alert("Addition: Correct");
scoreElement.value=score;

To

document.getElementById("score").value=score;

Also move all of your javascript to bottom of page (for example under: </body> tag) - thats the right way to do it. Also why you are loading jquery.js when you are not really using it? For example you could add all your javascript code to inside document ready. Like this:

$(function() {

    // your javascript code

});
Community
  • 1
  • 1
Mauno Vähä
  • 9,688
  • 3
  • 33
  • 54
  • Ah I initially was going to try just using Jquery but decided not too I'll remove that jquery. Whist your here might I ask you if you might know how to stop the timer from automatically counting down on page load? Sorry that its not related. – crmepham Mar 17 '13 at 14:19
  • Well, you are starting it yourself at line 84, see: var counter=setInterval(timer, 1000); make var count=60; and var counter; as global variables below score where its declared. Then inside timer() or some other method you start it, counter=setInterval(timer, 1000); and clear it when its needed. for example if count is 60 you call timer() again, when its 0 you stop it by clearing. These can be programmed inside timer() function checks. I would also use setTimeout instad of setInterval. – Mauno Vähä Mar 17 '13 at 14:27
1

you just need to declare the variable score like this outside the function checkAdd() {}

var score=0;

then it alerts also correct or incorrect, after you can display the score sorry, now I added the resetScore function

<form name="Addition">
        <table>
            <tr>
                <th>Addition</th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
            <tr>
                <th>Number 1</th>
                <th></th>
                <th>Number 2</th>
                <th></th>
                <th>Type Your Answer</th>
                <th>Correct Answer</th>
                <th>SCORE</th>
            </tr>
            <tr>
                <td>
                    <input type="text" name="AddN1" id="AddN1">
                </td>
                <td>+</td>
                <td>
                    <input type="text" name="AddN2" id="AddN2">
                </td>
                <td>=</td>
                <td>
                    <input type="text" name="AddA1" id="AddA1">
                </td>
                <td>
                    <input type="text" name="AddA2" id="AddA2">
                </td>
                <td>
                    <input type="text" name="score" id="score" value="0" readonly>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" value="Get Numbers" onclick="randomAdd();">
                <td></td>
                <td></td>
                <td></td>
                <td>
                    <input type="button" value="Check Answer" onclick="checkAdd();">
                </td>
                <td></td>
                <td>
                    <input type="button" value="Reset Score" onclick="resetScore();">
                </td>
            </tr>
        </table>
</form>
<script type="text/javascript">
var score=0;
function resetScore()
{   
    score=0;
    document.getElementById("score").value=score;
}
function randomAdd()
        {
            var x=document.getElementById("AddN1");
            x.value=Math.floor((Math.random()*12)+1);

            var y=document.getElementById("AddN2");
            y.value=Math.floor((Math.random()*12)+1);
        }


        function checkAdd()
        {
            var z=document.getElementById("AddA2");
            z.value= parseInt(document.getElementById("AddN1").value) + parseInt(document.getElementById("AddN2").value);

            if(parseInt(document.getElementById("AddA1").value)==z.value)
            {
                //score=score+1;
                score++;
                alert("Addition: Correct");
                document.getElementById("score").value=score;
            }
            else
            {   
                //score=score-1;
                score--;
                alert("Addition: Incorrect");
                document.getElementById("score").value=score;
            }
        }

</script>
caramba
  • 21,963
  • 19
  • 86
  • 127
  • Thanks for your reply, could you elaborate a bit more? – crmepham Mar 17 '13 at 13:33
  • why are you declaring score as string (score='') when you use it as int? – Mauno Vähä Mar 17 '13 at 13:40
  • For some reason it doesn't count past 1? – crmepham Mar 17 '13 at 13:40
  • @Mauno, sorry you're right! that was just at the beginning, after I forgot, I'll change ist. It was right in the functions. but thank you! – caramba Mar 17 '13 at 13:46
  • @crm, is it still no counting past one? was your addition right? – caramba Mar 17 '13 at 13:46
  • @caramba I have put the site online here: http://chrismepham.com/sites/website/addition.php perhaps you can see where I have gone wrong? I appreciate your help. – crmepham Mar 17 '13 at 13:53
  • for me it works. maybe there was something in the browser cache, or did you change something? try to add this at line 94 alert('Timer ended your score is '+score); – caramba Mar 17 '13 at 14:14
  • Thanks caramba Mauno V pointed out an issue with declaring the scoreElement variable before the DOM had loaded. Everything works okay now. – crmepham Mar 17 '13 at 14:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26325/discussion-between-caramba-and-crm) – caramba Mar 17 '13 at 14:47