Click to See Complete Forum and Search --> : problem calculating total on this form
Jeffrask
12-09-2003, 10:17 AM
Hi, I'm having a problem with this form; I can't get it to recognize the selected radio buttons as their respective values and the score remains 0. I've tried a lot of different configurations. Any help would be appreciated. I also suspect that their may be a much easier way of doing this.
Thanks again.
http://www.clickshine.com/brfit/quiz.htm
Pittimann
12-09-2003, 11:03 AM
Hi!
Your count function could be something like that:
function count()
{
for (var i = 0; i < document.MemoryQuiz.Q1.length; i++){
if (document.MemoryQuiz.Q1[i].checked) value1=parseInt(document.MemoryQuiz.Q1[i].value);
}
for (var i = 0; i < document.MemoryQuiz.Q2.length; i++){
if (document.MemoryQuiz.Q2[i].checked) value2=parseInt(document.MemoryQuiz.Q2[i].value);
}
for (var i = 0; i < document.MemoryQuiz.Q3.length; i++){
if (document.MemoryQuiz.Q3[i].checked) value3=parseInt(document.MemoryQuiz.Q3[i].value);
}
for (var i = 0; i < document.MemoryQuiz.Q4.length; i++){
if (document.MemoryQuiz.Q4[i].checked) value4=parseInt(document.MemoryQuiz.Q4[i].value);
}
document.MemoryQuiz.score.value = value1 + value2 + value3 + value4;
}
(Could be abbreviated more with two nested loops)
Cheers - Pit
Jeffrask
12-09-2003, 12:12 PM
Thanks Pit, I tried that here:
http://www.clickshine.com/brfit/quiz_pit.htm
But it still doesn't work. Am I missing something?
Pittimann
12-09-2003, 12:23 PM
Hi!
I checked your new link with Mozilla, Opera 6, IE 6 and NS 4.7 - no prob...
Cheers - Pit
jaegernaut
12-09-2003, 12:53 PM
I checked your link as well, and I do get error messages relating to value1 - value4 being undefined.
I believe this is because you reference them in the IF statement, but don't declare them anywhere.
I just added the var statements and I didn't get the errors.
Also, you may want to check the "Reset" function. It doesn't appear to clear the values back to 0. (you won't notice this if you answer all 4 each time)
Example: Select the first radio button on each question and click Calculate (should get 4). Now click Reset, and click on the second radio button for question 1 only (leave others blank), click Calculate again (score: 5)
Jeffrask
12-09-2003, 06:45 PM
Thank you both. It's working great now. I really appreciate your help.
Jeff