SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    Code:
    <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>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    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:

    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?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for the help. Your correct I'm trying to add current answer to sumtotal

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by faldini View Post
    Hi thanks for the help. Your correct I'm trying to add current answer to sumtotal
    So you would be wanting something like:

    Code javascript:
    $sumtotal = $sumtotal + $answer;

    or

    Code javascript:
    $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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by faldini View Post
    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.

    Code:
    $sumtotal = $answer;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •