...

View Full Version : function assistance



calavera
04-08-2003, 02:32 AM
I'm try to create a multiple singe question page (3 here) that uses a frameset in the index (purpose of storing data). Using *.js the quesions/answers are stored via which the question page will read the data and create the content, with the form buttons containing an onclick. I've assigned a function name checkit(). Once the user finishes there will be a results page.

My problem is creating a script checkit() that will compare and store the users choice, from the question file to qfile.js, allowing me to produce the results at a later date. Once I've got 1 page working right I reckon I'll be able to finish it off.
If anyone can help I'd appreciate it! :)

Cheers!
Cal

Copy of the code is below:


qfile1.js
---------

var testname = "Testing"
var numquestions = 3
var questions = new Array (numquestions)
var choices = new Array (numquestions)
for (var i=0; i<=numquestions; i++) choices[i] = new Array(5)
var correctanswers = new Array (numquestions)

question[1] = "Q1. ......................?"
choice[1][1] = "q1 Test answer1"
choice[1][2] = "q1 Test answer2"
choice[1][3] = "q1 Test answer3"
choice[1][4] = "q1 Test answer4"
choice[1][5] = "q1 Test answer5"
correct[1] = 4


index.htm
---------

<HTML>
<HEAD><BASE TARGET="content">
<SCRIPT SRC="qfile1.js"> </SCRIPT></HEAD>

<script>

function checkit() {
???????????????
}




</script>

<FRAMESET COLS="100%,*">
<FRAME SRC="question01.htm" NAME="question01"></FRAME>
</FRAMESET>


</HTML>



question01.htm
--------------

<HTML>
<HEAD></HEAD>

<BODY>

<SCRIPT>
document.write("<H1>" + parent.testname + "</H1>")
document.write("<P>" + parent.numquestions + " questions</P>")


</SCRIPT>

<FORM name=form1>
<script>
document.write("<P>" + parent.question[1] + "<BR>")
</script>
a. <INPUT onclick=parent.checkit() type=radio value=a name=q1>
<script>
document.write(parent.choice[1][1] + "<BR>")
</script>
b. <INPUT onclick=parent.checkit() type=radio value=b name=q1>
<script>
document.write(parent.choice[1][2] + "<BR>")
</script>
c. <INPUT onclick=parent.checkit() type=radio value=c name=q1>
<script>
document.write(parent.choice[1][3] + "<BR>")
</script>
d. <INPUT onclick=parent.checkit() type=radio value=d name=q1>
<script>
document.write(parent.choice[1][4] + "<BR>")
</script>
e. <INPUT onclick=parent.checkit() type=radio value=e name=q1>
<script>
document.write(parent.choice[1][5] + "<BR>")
</script>
<P></FORM>


<A HREF="results.htm">results</A>
</BODY>
</HTML>


result.htm
----------

no content yet.

cheesebagpipe
04-08-2003, 02:49 AM
Just right off the bat:

var questions = new Array (numquestions)
var choices = new Array (numquestions)
......
var correctanswers = new Array (numquestions)

question[1] = "Q1. ......................?"
choice[1][1] = "q1 Test answer1"
.......
correct[1] = 4

Keep those variable names consistent.

calavera
04-08-2003, 01:50 PM
ah yeah that would start to help :)

calavera
04-08-2003, 03:59 PM
I've corrected all the variable and I've been trying for a bit to correect the function that checks the users choice of answer to the answer stored in qfile1.js

I can't seem to solve it, I can get the function to see it but not compare. I think my code used for getting the users answer might be wrong.

The simple bit of code is below:

function checkit(){
if(parent.question01.document.form1.q1.value != parent.correctanswers[1]){
alert("wrong")
}
else {
alert("corret")
}

}

any ideas?
Cheers
Cal

cheesebagpipe
04-08-2003, 09:33 PM
Here's the problem: form1.q1 holds an array - that's what you get when you give more than one element the same name. You need to loop through it. Made a few changes...

var testname = "Testing"
var numquestions = 2
var questions = new Array (numquestions)
var choices = new Array (numquestions)
for (var i=0; i<numquestions; i++) choices[i] = new Array(5)
var correctanswers = new Object()

questions[0] = "Who's on first?"
choices[0][0] = "me"
choices[0][1] = "who"
choices[0][2] = "foo"
choices[0][3] = "Bruce"
choices[0][4] = "you"
correctanswers['q0'] = 1; //[group name] = choices[0][index]

questions[1] = "What is reality?"
choices[1][0] = "Everything."
choices[1][1] = "Nothing."
choices[1][2] = "Something."
choices[1][3] = "A hot pizza."
choices[1][4] = "I give up.."
correctanswers['q1'] = 3;

................................................................

<HTML>
<HEAD>
<BASE TARGET="content">
<SCRIPT SRC="qfile1.js"></SCRIPT>
<script>

function checkit(oRadio) {
var radgrp = oRadio.form[oRadio.name]; //get array
var correct_index = parent.correctanswers[oRadio.name]; //answer (group index)
var correct_rad = radgrp[correct_index]; //get correct radio
alert((correct_rad == oRadio) ? 'correct!' : 'wrong...'); //same as passed object?
}

</script>
</HEAD>
<FRAMESET COLS="100%,*">
<FRAME SRC="question01.htm" NAME="question01"></FRAME>
</FRAMESET>
</HTML>

................................................................

<HTML>
<HEAD></HEAD>
<BODY>
<SCRIPT>
document.write('<H1>' + parent.testname + '</H1>');
document.write('<P><b>[' + parent.numquestions + ' questions]</b></P>');
document.write('<FORM name="form1">');
var Qlength = parent.numquestions;
for (var i=0; i<parent.numquestions; i++) {
document.write('<P>' + (i + 1) + ') ' + parent.questions[i] + '</P>');
document.write('<ol type="a"><li>');
document.write('<INPUT onclick="parent.checkit(this)" type="radio" ');
document.write('value="a" name="q' + i + '">');
document.write(parent.choices[i][0] + '</li><li>');
document.write('<INPUT onclick="parent.checkit(this)" type="radio" ');
document.write('value="b" name="q' + i + '">');
document.write(parent.choices[i][1] + '</li><li>');
document.write('<INPUT onclick="parent.checkit(this)" type="radio" ');
document.write('value="c" name="q' + i + '">');
document.write(parent.choices[i][2] + '</li><li>');
document.write('<INPUT onclick="parent.checkit(this)" type="radio" ');
document.write('value="d" name="q' + i + '">');
document.write(parent.choices[i][3] + '</li><li>');
document.write('<INPUT onclick="parent.checkit(this)" type="radio" ');
document.write('value="e" name="q' + i + '">');
document.write(parent.choices[i][4] + '</li></ol>');
}
document.write('</form>');
</script>
<A HREF="results.htm">results</A>
</BODY>
</HTML>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum