Quote:
Originally Posted by hoshangc
Thanks jmrker. It works.
Can you please help me understand why we would need “var fnd = -1” for the option index of the selection? Also why did you choose -1?
Also, in question 2, when you click Check Answer without checking anything you get “That is CORRECT.”, which, of course, is incorrect. When you click check answer without checking anything in the other questions, you get “That is INCORRECT.”, which, of course is the right answer. So why is question 2 doing this? Can you please help explain this?
Thanks,
Hoshang
|
'Old Pedant's code is probably a bit more flexible in the long run,
but as I said before I was trying to use as much of your code as possible for you to see the changes I proposed.
If I had started from scratch, my own code might be different still.
Concerning the function in question, you should change it to this:
Code:
function getRBtnName(GrpName) {
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var i=0; i<sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
return fnd; // return option index of selection
// comment out next line if option index used in line above
// return str;
}
It returns the number of the radio button selection which, when checked, would return 0...(n-1) where (n-1) is the number of selections available.
The use of the -1 is not a value that will be returned if a selection is made.
If no selections are made, the return value is -1 which will NOT match any of the answers array elements, hence the 'incorrect' message.
In the original code, I neglected to change the return value of the function
to match the values you set in the answers array.
I was originally going to use the string returned in the function to match you original answer array contents.
I modified the answer array to make it easier to test, but forgot to alter the value returned by the function as well.
That is why it was giving the wrong message when the answer was analyzed.