Click to See Complete Forum and Search --> : Checking answers on multiple choice test


Pat Sanders
07-08-2003, 02:59 PM
I am trying to check the answers on a multiple choice test and display to the test taker in a javascript alert box what questions they got wrong and allow them to go back and correct the answers and resubmit. They can resubmit until all the answers are correct and them the form will email the information to the appropriate party.


How do I check the users answers against the key of correct answers and then display the message that Question 1 or 2 or whichever questions they got wrong is displayed in an alert box?:confused:

Jona
07-08-2003, 03:11 PM
You can append values to an array using the pop() method (array.pop(value)), I believe. Store all of the choices in an array, then compare them to an array that contains all of the correct answers.

[J]ona

Pat Sanders
07-09-2003, 04:39 PM
I am a javascript newbie and request some additional help.
I could not use the Pop that Jona suggested because I don't understand how if works.
I have changed my script and now have it verifying that the correct answers have been selected.
Is there a way that I can tell them which questions they got wrong instead of just the number they got wrong?

Also, I am having trouble getting the name and ssn to validate when I put the validation in my ValidateForm function. It skips the whole validate form - I have this code commented out right now. The ValidateForm() is in the form tag as an onSumbit event
onSubmit= "return ValidateForm()"

I know that this is not the most efficient way to code the quiz, but it's the only way I could figure out how to do it, because I still do not understand for loops either.

<SCRIPT LANGUAGE ="JavaScript">
<!--hide

function setFieldFocus(field){
field.focus(); //Bring focus to form field
field.select(); //Select form field
return false;
}
function ValidateForm(){

//Make sure Name is entered
// if(document.form1.name.value.length == 0) {
// alert("Please enter your Name.");
// document.form1.name.focus();
// return false;
// }
//Make sure SSN is entered
// if(document.form1.ssn.value.length == 0) {
// alert("Please enter your Social Security Number.");
// document.form1.ssn.focus();
// return false;
// }

//return true;

var a = 0;
var b = 0;
var c = 0;
var d = 0;

if(document.forms[0].one[2].checked==true){
a = 1;
}
if(document.forms[0].two[1].checked==true){
b = 1;
}
if(document.forms[0].three[0].checked==true){
c = 1;
}
if(document.forms[0].four[2].checked==true){
d = 1;
}

total = (a + b + c + d);

if(total == 4) {
alert("You have answered all the questions correctly. Thank you for completing the Financial Aid Online Counseling.");
}
if(total < 4) {
alert("You answered " + total + " out of 4 correct. Please try again!");
}

}

//stop hiding-->

</SCRIPT>

Any assistance would be greatly appreciated!

Jona
07-09-2003, 04:47 PM
if(total < 4) {
switch(total){
case a: result = document.forms[0].one[2].value; break;
case b: result = document.forms[0].two[1].value; break;
case c: result = document.forms[0].three[0].value; break;
case d: result = document.forms[0].four[2].value; break;
}
alert("You answered " + total + " out of 4 correct. Your answer was "+result+" Please try again!");
}


Not sure if it will work, but it's worth a try.

[J]ona

Pat Sanders
07-10-2003, 08:41 AM
I thank you for you help, but it will only give me one question that was wrong. Probably because the break after each statement.

Jona
07-10-2003, 12:26 PM
Looks like it'll need a for() loop. Here goes some untested code...


if(total < 4) {
result = new Array();
for(i=0; i<total; i++){
switch(total){
case a: result[i] = document.forms[0].one[2].value; break;
case b: result[i] = document.forms[0].two[1].value; break;
case c: result[i] = document.forms[0].three[0].value; break;
case d: result[i] = document.forms[0].four[2].value; break;
} }
alert("You answered " +total+ " out of 4 correct. Your answer was "+result+" Please try again!");
}


[J]ona

olerag
07-10-2003, 02:17 PM
The code below does a silly test using radio buttons for selections. The incorrect results show the affected question(s) and what the correct answer should be.

I didn't see any need to show the incorrect answer values since this is already in front of the user. This isn't a very dynamic example but, hell, I'm not a very dynamic person.

<html>
<head>
<SCRIPT Language ="JavaScript">

function ValidateForm(){
result = new Array();
count = 0;

if (!document.forms[0].one[2].checked == true) {
result[count] = "Question 1. The correct answer is " + document.forms[0].one[2].value + ".";
count++;
}

if (!document.forms[0].two[1].checked == true) {
result[count] = "Question 2. The correct anwser is " + document.forms[0].two[1].value + ".";
count++;
}

if (!document.forms[0].three[0].checked == true) {
result[count] = "Question 3. The correct answer is " + document.forms[0].three[0].value + ".";
count++;
}

if (!document.forms[0].four[2].checked == true) {
result[count] = "Question 4. The correct answer is " + document.forms[0].four[2].value + ".";
count++;
}

if (result.length > 0) {
var vString = "";
for (var i=0; i<result.length; i++) {
vString = vString + result[i] + "\n";
}
alert(result.length + " incorrect answers:\n" + vString);
return(false);
}
else {
alert("All of the questions were answered correctly so submit email notice.");
return(true);
}
}
</SCRIPT>

</head>
<body>
<form>
<center>
<b>Sample Quiz</b>
</center>

<p>
Question 1: Who hit the most home runs?
<br>
<input type="radio" name="one" value="Babe Ruth">Babe Ruth
<input type="radio" name="one" value="Bobby Bonds">Bobby Bonds
<input type="radio" name="one" value="Hank Aaron" checked>Hank Aaron
<input type="radio" name="one" value="Willie Mays">Willie Mays

<p>
Question 2: In what state is New York City located?
<br>
<input type="radio" name="two" value="Maryland">Babe Ruth
<input type="radio" name="two" value="New York" checked>New York
<input type="radio" name="two" value="Virginia">Virginia
<input type="radio" name="two" value="California">California

<p>
Question 3: Who was the first president of the United States?
<br>
<input type="radio" name="three" value="George Washington" checked>George Washington
<input type="radio" name="three" value="Dick Nixon">Dick Nixon
<input type="radio" name="three" value="Abe Lincoln">Abe Lincoln
<input type="radio" name="three" value="Theodore Roosevelt">Theodore Roosevelt

<p>
Question 4: Who wrote the story, "Tom Sawyer"?
<br>
<input type="radio" name="four" value="Edgar Allan Poe">Edgar Allan Poe
<input type="radio" name="four" value="Tom Clancy">Tom Clancy
<input type="radio" name="four" value="Mark Twain" checked>Mark Twain
<input type="radio" name="four" value="Charles Dickens">Charles Dickens

<center>
<p>
<input type="button" value="Submit" onClick="ValidateForm()">
</center>
</form>
Considering that the correct answers are already checked, this isn't much
of a quiz.
</body>
</html>

Pat Sanders
07-10-2003, 03:05 PM
Thanks Olerag!

I modified the code to only give them the Question number since I did not want to give them the answer and I did not automatically check any of my radio buttons and it works perfectly!

Thanks again!!!!!:D