PDA

View Full Version : Sum of int not working

renzocj
12-26-2011, 11:06 PM
This is not working, any idea ?

function respuestas1() {
var values;
for (var i=0; i<radios.length; i++) {
sum = 0;
for (var i=0; i<values.length; i++) {
sum = sum + Number(values[i]);
}
}
}
}

Apothem
12-26-2011, 11:11 PM
It may have something to do with the fact that you're using a triple equal "operator" (I believe its not a valid operator).

renzocj
12-26-2011, 11:13 PM
It's the "strict equal operator" and only returns a Boolean true if both the operands are equal and of the same type.

Apothem
12-26-2011, 11:26 PM
Didn't think JS had that. Well, the other "problem" I see is that sum was not declared as a variable.

renzocj
12-26-2011, 11:30 PM
Function still not working, the result is a eternal loop-

Apothem
12-26-2011, 11:37 PM
Ah right. I can't believe I forgot that. Your inner loop is reusing the variable "i". Change it to something like "j".

jmrker
12-26-2011, 11:43 PM
Try this...

function respuestas1() {
var sum = 0;
for (var i=0; i<radios.length; i++) {
}
}
return sum;
}

renzocj
12-26-2011, 11:57 PM
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!

Apothem
12-27-2011, 12:01 AM

jmrker
12-27-2011, 12:03 AM
Nearly the same code...

function respuestas1() {
var sum = 0;
for (var i=0; i<radios.length; i++) {