Problem saving variables to sumtotal variable

Hi guys,
I’m trying to save my answer variable to a sumtotal variable but I am getting the wrong answer to sumtotal at the moment. Could someone please have a look and point me in the right direction.
thanks, gordon

<script type="text/javascript">
				var $randomnumber1;
				var $randomnumber2;
				var $sumtotal = 0;
				var $answer;			// Answers to the questions.
				var $counter = 1;
				var $data;
				// Counter value displays 1 on press of start.
				
		
		function start_game() {	
				document.getElementById('ans').innerHTML = "";
			 document.getElementById('counternumber').innerHTML = $counter;
			 document.getElementById('txtbox').value = "";					/*clears the textbox of any text */
			 $randomnumber1 = Math.floor(Math.random()*11); 				/*generate random num, num entered into $randomnumber*/
			 $randomnumber2 = Math.floor(Math.random()*11);
					
				document.getElementById('num1').innerHTML = $randomnumber1; /* will display num on screen in H3*/
				document.getElementById('num2').innerHTML = $randomnumber2;	
					$answer = $randomnumber1 + $randomnumber2;
					$sumtotal = $answer;
					$counter++;	
					setFocus();	
		}			
		
		function check_answer() {
				var $txt = document.getElementById('txtbox'); /* gets text box entry, saves it to $txt*/
				var $value = $txt.value;
					if ($value == $answer) {
						alert('You are correct');
						//addimage();
					}
					else {
						alert('You are incorrect, the answer was ' + $answer);
					}
					addAnswers();
				document.getElementById('txtbox').value = "";				/*clears the textbox of any text */
				document.getElementById('num1').innerHTML = "";
				document.getElementById('num2').innerHTML = "";	
						 $randomnumber1 = Math.floor(Math.random()*11); /*generate random num, num entered into $randomnumber*/
						 $randomnumber2 = Math.floor(Math.random()*11);
						 document.getElementById('num1').innerHTML = $randomnumber1; /* will display num on screen in H3*/
						 document.getElementById('num2').innerHTML = $randomnumber2;
							$answer = $randomnumber1 + $randomnumber2;
							$sumtotal = $answer;
					document.getElementById('counternumber').innerHTML = $counter;
					$counter++;	
						if ($counter > 4) {
							//alert ('End of game......Thanks for playing');
							mytotal();
								document.getElementById('num1').innerHTML = "";
								document.getElementById('num2').innerHTML = "";
									$counter = 1;
										document.getElementById('counternumber').innerHTML = "";
											
								
						}	
		
		}
		function mytotal() {
			 document.getElementById('total').innerHTML = $sumtotal;
		}
		function setFocus() {
			var mytext = document.getElementById("txtbox");
			mytext.focus();
		}
			onload = focusIt;
			
		function addimage() {
			var img = document.createElement('img');
			img.src = "images/correct.png";
			document.body.appendChild(img);
		}
		function addAnswers() {
            var data = $randomnumber1 + " + " + $randomnumber2 + " = " + ($randomnumber1+$randomnumber2);
            var newListItem = document.createElement('li');
			// maybe append img tag to li here
            var newText = document.createTextNode(data);
					newListItem.appendChild(newText);
					document.getElementById("ans").appendChild(newListItem);
        }		
		</script>

The first problem that I see is that you haven’t provided any HTML code to help us troubleshoot your problem.
So, I’ve created some temporary HTML code:


<div>Num 1: <span id="num1"></span></div>
<div>Num 2: <span id="num2"></span></div>
<p><input id="txtbox" /></p>
<div>Total: <span id="total"></span></div>
<div>Ans: <span id="ans"></span></div>
<div>Counternumber: <span id="counternumber"></span></div>

The first thing I notice when running the code, is a complaint from the JavaScript console that “Uncaught ReferenceError: focusIt is not defined”
I presume that focusIt should be setFocus?

I also notice that some of the variables start with a dollar sign. That’s very distracting, because normally that is only done to indicate that the variable contains a jQuery-specific object instead.

With the answer, you are assigning to sumtotal only the result of the current answer. I suspect that you want to instead add the current answer to the existing sumtotal, right?

Hi thanks for the help. Your correct I’m trying to add current answer to sumtotal

So you would be wanting something like:


$sumtotal = $sumtotal + $answer;

or


$sumtotal += $answer;

But those variables contain strings, so the numbers won’t add up as you expect. Because, when things first begin the sumtotal variable is undefined. And undefined plus something else is also undefined.

Start off by defining a starting number for the sumtotal, such as 0, and you should be fine.

I under stand what your saying about sum total paul but I did define it at the start of the script, sumtotal = 0. But when I ran it I kept getting odd answers. So are you saying the reason for the odd answers is because of document.getElementById(‘num1’).innerHTML = $randomnumber1. And this is returning the Varitable as a string.

Nope, not at all.

Currently the reason for the problem is because you are replacing the sumtotal with the most recent answer.


$sumtotal = $answer;