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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script won't correctly grade my exam 1

Status
Not open for further replies.

DrAsh

Technical User
May 3, 2001
46
US
here's a more specific question on my page. I have an exam (T/F with radio buttons). I created a script to grade it followed by a function to add the exam takers name to a certificate if they score hirer than 90%. But It will not grade. My programming knowledge is limited so please help. Here's the script, fairly basic actually:

<script language=&quot;JAVASCRIPT&quot;>
function grading(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10){
var right = 0;
var passed = 0;

prompt (a1)
if (a1 == &quot;CORRECT&quot;){
right = (right + 1);
}
if (a2 == &quot;CORRECT&quot;){
right = (right + 1);
}
if (a3 == &quot;CORRECT&quot;){
right = (right + 1);
}
if (a4 == &quot;CORRECT&quot;){
right = (right + 1);
}
if (a5 == &quot;CORRECT&quot;){
right = (right + 1);
}
if (a6 == &quot;CORRECT&quot;){
right = (right + 1);
}
if (a7 == &quot;CORRECT&quot;){
right = (right + 1);
}
if (a8 == &quot;CORRECT&quot;){
right = (right + 1);
}
if (a9 == &quot;CORRECT&quot;){
right = (right + 1);
}
if (a10 == &quot;CORRECT&quot;){
right = (right + 1);
}

if (right == 9){
passed = 1;
}
if (right == 10){
passed = 1;
}
if (passed == 1){
name_on_certificate.value == test_taker.value;
}
if (passed == 0){
prompt(&quot;You Failed and You must retake test. Hit cancel and refresh&quot;);
}
}
</script>

*****(AND THIS IS THE FUNCTION THAT CALLS UP THE SCRIPT)***

<input type=&quot;button&quot; value=&quot;grader2&quot; name=&quot;grader2&quot;
onclick=&quot;grading(Q1.value, Q2.value, Q3.value, Q4.value, Q5.value, Q6.value, Q7.value, Q8.value, Q9.value, Q10.value)&quot;></p>
</center></div><div align=&quot;center&quot;><center><p><a href=&quot;#top&quot;>[Back to top]</a> </p>
</center></div></div>

 
Some browsers, notably IE5.5, seem to have trouble with the value attribute of radio buttons. You may want to just pass the button reference itself, and use button[0].checked. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Are your &quot;Q&quot; numbers your radio buttons? If so try change your function to this..


function grading(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10){
var right = 0;
var passed = 0;


if (a1)
right+=1;
if (a2)
right+=1;
if (a3)
right+=1;
if (a4)
right+=1;
if (a5)
right+=1;
if (a6)
right+=1;
if (a7)
right+=1;
if (a8)
right+=1;
if (a9)
right+=1;
if (a10)
right+=1;

if (right == 9)
passed = 1;

if (right == 10)
passed = 1;

if (passed == 1)
window.name_on_certificate.value = window.test_taker.value;

if (passed == 0)
alert(&quot;You Failed and You must retake test. Hit cancel and refresh&quot;);

}

Klae

You're only as good as your last answer!
 
Klae, I inserted your script and it seems to work. But the problem is if the answers are correct, it still promtps the alert, you failed. So it isn't grading properly. But your script did get me furthur than before. In terms of the value :

if (passed == 1)
window.name_on_certificate.value = window.test_taker.value;

it doesn't get to that point since the grader doesn't grade properly.

The radio button are given values as &quot;CORRECT&quot; or &quot;INCORRECT&quot;. Q1=correct, add 1 If Q2 = incorrect, don't add anything. The correct and incorrect are the value assigned to each radio button so I don't know why it won't grader properly.




Also, can someone explain to me the
window.name_on_certificate.value = window.test_taker.value;






 
I assume you assign the value &quot;CORRECT&quot; to whichever of the radio buttons contains the correct value. So you will have to subscript each question's radio button set to the one which is the correct answer and see if it is checked.
Code:
if ( a1[0].checked ) // first button is correct answer
...
if ( a2[2].checked ) // third button is correct answer
Also you can reduce all the ifs at the end to:
Code:
if ( right >= 9 )  {
    name_on_certificate.value = test_taker.value;
} else {
    alert(&quot;You failed and Your must retake test. Hit Cancel and Refresh.&quot;)
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Sorry I seemed to of thought that you were so new to javascripting that you thought &quot;Correct&quot; was the same as True.

Try this code

Code:
function grading()
{
var right=0
 for (i=1;i<=10;i++)
 {
  if (eval(&quot;Q&quot; + i + &quot;.value&quot;) == &quot;CORRECT&quot;)
   right+=1
 }
if (right >= 9)
 name_on_certificate.value = test_taker.value;
else
 alert(&quot;You failed and Your must retake test. Hit Cancel and Refresh.&quot;)
}

This should work.

Here is my complete test page. (For test purposes I have 10 radio buttons and every one has a name of &quot;CORRECT&quot;.

Code:
<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
<TITLE></TITLE>
<script language=&quot;JavaScript&quot;>
function grading()
{
var right=0;
 for (i=1;i<=10;i++)
 {
  if (eval(&quot;Q&quot; + i + &quot;.value&quot;) == &quot;CORRECT&quot;)
   right+=1;
 }
if (right >= 9)
 name_on_certificate.value = test_taker.value;
else
 alert(&quot;You failed and You must retake test. Hit Cancel and Refresh.&quot;);
}

</script>
</HEAD>
<BODY>

<P><INPUT id=name_on_certificate name=name_on_certificate>
<INPUT id=test_taker name=test_taker>
<INPUT id=&quot;Q1&quot; name=&quot;Q1&quot; value=&quot;CORRECT&quot; type=radio>
<INPUT id=&quot;Q2&quot; name=&quot;Q2&quot; value=&quot;CORRECT&quot; type=radio>
<INPUT id=&quot;Q3&quot; name=&quot;Q3&quot; value=&quot;CORRECT&quot; type=radio>
<INPUT id=&quot;Q4&quot; name=&quot;Q4&quot; value=&quot;CORRECT&quot; type=radio>
<INPUT id=&quot;Q5&quot; name=&quot;Q5&quot; value=&quot;CORRECT&quot; type=radio>
<INPUT id=&quot;Q6&quot; name=&quot;Q6&quot; value=&quot;CORRECT&quot; type=radio>
<INPUT id=&quot;Q7&quot; name=&quot;Q7&quot; value=&quot;CORRECT&quot; type=radio>
<INPUT id=&quot;Q8&quot; name=&quot;Q8&quot; value=&quot;CORRECT&quot; type=radio>
<INPUT id=&quot;Q9&quot; name=&quot;Q9&quot; value=&quot;CORRECT&quot; type=radio>
<INPUT id=&quot;Q10&quot; name=&quot;Q10&quot; value=&quot;CORRECT&quot; type=radio>
<INPUT id=&quot;btn1&quot; value=&quot;Grade&quot; type=button onClick=&quot;grading();&quot;>
</P>

</BODY>
</HTML>

Klae

You're only as good as your last answer!
 
the window.name_on_certificate.value is just me! in IE that is the full property name but you can get away with just name_on_certificate.value. Klae

You're only as good as your last answer!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top