Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-26-2011, 10:06 PM   PM User | #1
renzocj
New Coder

 
Join Date: Sep 2011
Location: Lima, Peru
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
renzocj is an unknown quantity at this point
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);
			}
		}
	}
}
renzocj is offline   Reply With Quote
Old 12-26-2011, 10:11 PM   PM User | #2
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
It may have something to do with the fact that you're using a triple equal "operator" (I believe its not a valid operator).
Apothem is offline   Reply With Quote
Old 12-26-2011, 10:13 PM   PM User | #3
renzocj
New Coder

 
Join Date: Sep 2011
Location: Lima, Peru
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
renzocj is an unknown quantity at this point
It's the "strict equal operator" and only returns a Boolean true if both the operands are equal and of the same type.
renzocj is offline   Reply With Quote
Old 12-26-2011, 10:26 PM   PM User | #4
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
Didn't think JS had that. Well, the other "problem" I see is that sum was not declared as a variable.
Apothem is offline   Reply With Quote
Old 12-26-2011, 10:30 PM   PM User | #5
renzocj
New Coder

 
Join Date: Sep 2011
Location: Lima, Peru
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
renzocj is an unknown quantity at this point
Function still not working, the result is a eternal loop-
renzocj is offline   Reply With Quote
Old 12-26-2011, 10:37 PM   PM User | #6
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
Ah right. I can't believe I forgot that. Your inner loop is reusing the variable "i". Change it to something like "j".
Apothem is offline   Reply With Quote
Old 12-26-2011, 10:43 PM   PM User | #7
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
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;
}
jmrker is offline   Reply With Quote
Old 12-26-2011, 10:57 PM   PM User | #8
renzocj
New Coder

 
Join Date: Sep 2011
Location: Lima, Peru
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
renzocj is an unknown quantity at this point
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!
renzocj is offline   Reply With Quote
Old 12-26-2011, 11:01 PM   PM User | #9
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
Divide the sum by radios.length
Apothem is offline   Reply With Quote
Old 12-26-2011, 11:03 PM   PM User | #10
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
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.
jmrker is offline   Reply With Quote
Reply

Bookmarks

Tags
array

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:12 PM.


Advertisement
Log in to turn off these ads.