Creating An Online Quiz
Getting The Answers
The code for getting the answers which the client has chosen is made up from two functions. Functions are sections of script which are defined by the author of the code for example;
function writeMyName(){
document.write('My Name');
}
If you were to put the above code into your JavaScript and then further down use the line;
writeMyName();
The screen would display the text "My Name". You've created a function to execute the script contained within the "{}".

The first code, 'anscheck', reads which answer has been selected for the question;
function anscheck(bute){
firstbuttonsel=eval("document.formwun.selected"+bute+"[0].checked");
secondbuttonsel=eval("document.formwun.selected"+bute+"[1].checked");
if (firstbuttonsel){
runnintotal=runnintotal + Number(valA[bute])}
else
if (secondbuttonsel){
runnintotal=runnintotal + Number(valB[bute])}
}
Line one defines 'anscheck' (Answer Check) as our function and it is to take any value which is placed in the brackets and pass it to the variable 'bute' (can't remember why I used the name 'bute'). This particular function will be called from a loop which passes the value of 'i' to the 'bute' variable, therefore, the first time the function is run in the loop 'i' = 1 so 'bute'=1 .

Line two sets the variable 'firstbuttonsel'. The 'eval' function combines the text "document.formwun.selected", the variable 'bute' and the text "[0].checked" to give the JavaScript object 'document.formwun.selected1[0].checked' (where 'bute' is equal to one).

Line three sets the variable 'secondbuttonsel'. The 'eval' function combines the text "document.formwun.selected", the variable 'bute' and the text "[1].checked" to give the JavaScript object 'document.formwun.selected1[1].checked' (where 'bute' is equal to one).

Line four is the head of an 'if' statement and checks to see if 'firstbuttonsel' is true. If the first answer is selected then 'firstbuttonsel' evaluates true and the next line will run.

Line five adds the numeric value of 'valA' to the running total for the testee.

Line six simply states that if you're not doing the above then move on to the next line.

Line seven is the head of an 'if' statement and checks to see if 'secondbuttonsel' is true. If the second answer is selected then 'secondbuttonsel' evaluates true and the next line will run.

Line eight adds the numeric value of 'valB' to the running total for the testee.

The next section of code (ansdisplay) is that which is directly run by the form button. This currently displays the total score and then resets the score, it does not reset the radio buttons, neither does it change the page, therefore you can keep trying the quiz until you get it right and you'll only leave the page when you select a different button. This part of code is what will next need working on to give a different result to hitting the "submit answer" button.
function ansdisplay(){
for (var i=1; i<qnum+1; i++)
{
anscheck(i);
}
alert(runnintotal);
runnintotal=0
}
Line one defines the function 'ansdisplay' which has no operators within the brackets.

Line two is the start of a 'for' loop which will run for each question (as defined in quiz.js under 'qnum').

Line three says to run the 'anscheck' function and pass the value of 'i' on to the function.

Line four displays the value of 'runnintotal' in an alert box.

Line five resets the running total to zero to start again.

The next step will be to make the code do something more than just display the alert box.


<<< LAST PAGE   NEXT PAGE >>>
BACK TO HOME PAGE

Valid HTML 4.01!