Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2011
    Location
    Lima, Peru
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Sum of int not working

    This is not working, any idea ?

    Code:
    function respuestas1() {
    	var radios=document.getElementsByTagName("input");
    	var values;
    	for (var i=0; i<radios.length; i++) {
    		if (radios[i].type==="radio" && radios[i].checked) {
    			values=radios[i].value;
    			sum = 0;
    			for (var i=0; i<values.length; i++) {
    				sum = sum + Number(values[i]);
    				alert(sum);
    			}
    		}
    	}
    }

  • #2
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    It may have something to do with the fact that you're using a triple equal "operator" (I believe its not a valid operator).

  • #3
    New Coder
    Join Date
    Sep 2011
    Location
    Lima, Peru
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It's the "strict equal operator" and only returns a Boolean true if both the operands are equal and of the same type.

  • #4
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Didn't think JS had that. Well, the other "problem" I see is that sum was not declared as a variable.

  • #5
    New Coder
    Join Date
    Sep 2011
    Location
    Lima, Peru
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Function still not working, the result is a eternal loop-

  • #6
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Ah right. I can't believe I forgot that. Your inner loop is reusing the variable "i". Change it to something like "j".

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Try this...
    Code:
    function respuestas1() {
      var radios=document.getElementsByTagName("input");
      var sum = 0;
      for (var i=0; i<radios.length; i++) {
        if ((radios[i].type=="radio") && (radios[i].checked)) {
          sum += Number(radios[i].value);  // assumes numbers are in radio button values
        }
      }
      alert(sum);
      return sum;
    }

  • #8
    New Coder
    Join Date
    Sep 2011
    Location
    Lima, Peru
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you now I have the total sum, just in case do you know what I would have to do if I wanted to have an average ? Let's say I have more groups of radio buttons ...

    Thanks for all the support!

  • #9
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Divide the sum by radios.length

  • #10
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Nearly the same code...
    Code:
    function respuestas1() {
      var radios=document.getElementsByTagName("input");
      var sum = 0;
      for (var i=0; i<radios.length; i++) {
        if ((radios[i].type=="radio") && (radios[i].checked)) {
          sum += Number(radios[i].value);  // assumes numbers are in radio button values
        }
      }
      var average = sum / (radios.length+1);  // assumes length value is NOT zero
      alert('Sum is: '+sum+'\nAverage: '+average);
      return sum;
    }
    If you need only the averages of the buttons checked, that could be done as well.
    Give it a shot yourself if you want to learn.
    If not, then just post "I give up!"

    Major problem with averages of radio buttons is that only ONE button can be checked in a group.
    You would need to change to checkboxes if you want an average of more than one button.


  •  

    Tags for this Thread

    Posting Permissions

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