Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with this quiz Validation script 2

Status
Not open for further replies.

jaslr

Programmer
Aug 16, 2001
67
AU
I have been using the below script to validate a 12 question quiz. The questions have 2 answers each, today I am making each question have 4 multiple choice answers,however the below script doesn't check the 2 extra multiple choice answers and hence can't continue.

How can I make the below script check for all 4 answers instead of 2 it presently is, I know it' around this part somewhere but am too much of a javascript n00b to remember:

Code:
  var yesChoice = eval("document.quizForm.q" + i + "[0].checked");
        var noChoice  = eval("document.quizForm.q" + i + "[1].checked"); 
        var noAnswer  = eval("document.quizForm.q" + i + "[0].value");
        var yesAnswer = eval("document.quizForm.q" + i + "[1].value");


Code:
<SCRIPT LANGUAGE="JavaScript"><!--
function validateAnswers() {
    var correct = 0;
    var wrong = 0;
    var blank = 0;
    for (var i=1;i<13;i++) {
        var yesChoice = eval("document.quizForm.q" + i + "[0].checked");
        var noChoice  = eval("document.quizForm.q" + i + "[1].checked"); 
        var noAnswer  = eval("document.quizForm.q" + i + "[0].value");
        var yesAnswer = eval("document.quizForm.q" + i + "[1].value");

        if (yesChoice == noChoice)
            blank++; // can't both be checked, thus must be both unchecked
        else {
            if ((yesChoice.toString() == yesAnswer) &&
                (noChoice.toString() == noAnswer))
                correct++;
            else
                wrong++;
        }
    }
    document.quizForm.correct.value = correct;
    document.quizForm.wrong.value = wrong;
    document.quizForm.blank.value = blank;
}
//--></SCRIPT>
</body>
 
var yesChoice = eval("document.quizForm.q" + i + "[0].checked");
var noChoice = eval("document.quizForm.q" + i + "[1].checked");

var yesChoice1 = eval("document.quizForm.q" + i + "[2].checked");
var noChoice1 = eval("document.quizForm.q" + i + "[3].checked");

var yesChoice1 will have the next set's first choice. i suggest u use someother method rather than hardcoding it...

Known is handfull, Unknown is worldfull
 

Consider getting rid of the eval commands. They're outdated, slow, and very unnecessary.

You can replace this syntax:

Code:
eval("document.quizForm.q" + i + "[0].checked");

with this:

Code:
document.forms['quizForm'].elements['q' + i][0].checked;

Hope this helps,
Dan
 
Dan,
i still use eval commands a lot, why r they so messy???

Known is handfull, Unknown is worldfull
 

eval calls have a latency attached to their use, as they have to do just what the name implies - evaluate whetever expression is given.

For example, if you wanted to do:

Code:
alert(eval('document.forms[0].elements[q' + i + '].value'));

The eval would first make the complete string. Lets assume that i is equal to 1, that would make:

Code:
alert(eval('document.forms[0].elements[q1].value'));

Because eval can evaluate anything, not just form element calls, it them has to further parse the string you've entered. It is this that takes the time.

Take this test harness. All it does is query the value of a form element 100,000 times:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		function testTimes() {
			var numIterations = 100000;
			var outputHTML = '';

			var startTime = new Date().getTime();
			for (var loop=0; loop<numIterations; loop++) var testValue = document.forms[0].elements['test1'].value;
			var endTime = new Date().getTime();
			outputHTML += 'Time taken without eval for ' + numIterations + ' iterations: ' + ((endTime-startTime)/1000) + ' seconds';
			outputHTML += '<br />';

			var startTime = new Date().getTime();
			for (var loop=0; loop<numIterations; loop++) var testValue = eval('document.forms[0].elements[\'test' + 1 + '\'].value');
			var endTime = new Date().getTime();
			outputHTML += 'Time taken with eval for ' + numIterations + ' iterations: ' + ((endTime-startTime)/1000) + ' seconds';

			document.getElementsByTagName('body')[0].innerHTML = outputHTML;
		}
	//-->
	</script>
</head>
<body onload="testTimes();">
	<form>
		<input type="hidden" name="test1" value="Test" />
	</form>
</body>
</html>

Without eval, it takes 3.7 seconds. With eval, it takes 12 seconds. A huge difference, I'm sure you'll agree.

Hope this helps,
Dan
 
thanks guys, will try it when I get back to work next week

dance.gif
 
o.k, got it...

Known is handfull, Unknown is worldfull
 
and a * too, my stuoid computer threw up before i could give u one...

Known is handfull, Unknown is worldfull
 
ok, actually no one has really answerd my question completely yet. I have replaced my eval with ocument.forms['quizForm'].elements['q' + i][0].checked;

but it still isn't checking the 3rd and 4th option?
 

I have replaced my eval with ocument.forms['quizForm'].elements['q' + i][0].checked;

Did you mean "document" or "ocument"? "ocument" would definately not work.

Can you post the new code that you have now?

Dan
 
<SCRIPT LANGUAGE="JavaScript"><!--
function validateAnswers() {
var correct = 0;
var wrong = 0;
var blank = 0;
for (var i=1;i<13;i++) {
var yesChoice = eval("document.quizForm.q" + i + "[0].checked");
var noChoice = eval("document.quizForm.q" + i + "[1].checked");
var noAnswer = eval("document.quizForm.q" + i + "[0].value");
var yesAnswer = eval("document.quizForm.q" + i + "[1].value");


if (yesChoice == noChoice)
blank++; // can't both be checked, thus must be both unchecked
else {
if ((yesChoice.toString() == yesAnswer) &&
(noChoice.toString() == noAnswer))
correct++;
else
wrong++;

}
}
document.quizForm.correct.value = correct;
document.quizForm.wrong.value = wrong;
document.quizForm.blank.value = blank;
}
//-->
</SCRIPT>
 

You haven't posted the HTML code... Can you post that? If it's not too long, post the whole thing - otherwise a URL would be good. It's hard to debug HTML & JS when we've only got the JS!

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top