Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Sum of int not working

1. ## Sum of int not working

This is not working, any idea ?

Code:
```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]);
}
}
}
}```

2. 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. It's the "strict equal operator" and only returns a Boolean true if both the operands are equal and of the same type.

4. Didn't think JS had that. Well, the other "problem" I see is that sum was not declared as a variable.

5. Function still not working, the result is a eternal loop-

6. Ah right. I can't believe I forgot that. Your inner loop is reusing the variable "i". Change it to something like "j".

7. Try this...
Code:
```function respuestas1() {
var sum = 0;
for (var i=0; i<radios.length; i++) {
}
}
return sum;
}```

8. 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. Divide the sum by radios.length

10. Nearly the same code...
Code:
```function respuestas1() {
var sum = 0;
for (var i=0; i<radios.length; i++) {
}
}
var average = sum / (radios.length+1);  // assumes length value is NOT zero
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.