...

View Full Version : need help with quiz script



mayankdb
06-04-2010, 06:48 AM
hello everyone,
first a confession-i have a very VERY superficial understanding of javascript and other programing languages. i am a teacher, preparing a small quiz that my students can take online.
i found a free script code on the net, but there's a small problem - i want to give marks for every correct answer in the quiz but penalize for every incorrect answer. the ready-made code already gives marks for the correct answer, but does not penalize the incorrect answer. the part of the code is as under

code begins....



function gradeit(){
var incorrect=null
for (q=1;q<=totalquestions;q++){
var thequestion=eval("document.myquiz.question"+q)
for (c=0;c<thequestion.length;c++){
if (thequestion[c].checked==true)
actualchoices[q]=thequestion[c].value
}

if (actualchoices[q]!=correctchoices[q]){ //process an incorrect choice
if (incorrect==null)
incorrect=q
else
incorrect+="/"+q
}
}

if (incorrect==null)
incorrect="a/b"
document.cookie='q='+incorrect
if (document.cookie=='')
alert("Your browser does not accept cookies. Please adjust your browser settings.")
else
window.location="results.htm"
}


...code ends

many thanks in advance.

-mdb

kandryson
06-04-2010, 08:30 AM
i just now read this post this is very nice post i like this post and very influence with this post
Advanced colon max (http://www.articlesbase.com/health-articles/advanced-colon-max-review-amp-free-trial-2146370.html)

Dormilich
06-04-2010, 08:56 AM
tip:
instead of eval("document.myquiz.question"+q) use document.myquiz["question"+q]

I’d like to see a representative HTML part, because I think there is a lot of potential to optimise the script.

mayankdb
06-04-2010, 12:38 PM
Dormilich, many thanks.

as i said, i am a complete novice so i didn't understand what you meant by representative HTML. Perhaps i'll elaborate?

there are three files, (1) popquiz.htm (2) quizconfig.js (3) results.htm ... as would be obvious to you, the popquiz.htm contains the questions to the test, quizconfig does the processing and the results.htm displays the result (pl don't be offended if this detailing sounds condescending to you... i'm assuming that people need to be explained in as much detail as they need to tell me :)

could you specify which part / file should i paste here?
-mdb

Dormilich
06-04-2010, 12:56 PM
as i said, i am a complete novice so i didn't understand what you meant by representative HTML. Perhaps i'll elaborate?

quite often it is sufficient to only see a part of the page, e.g. if you have 20 adequate question blocks, seeing one is enough to understand, how each works.


don't be offended if this detailing sounds condescending to you... i'm assuming that people need to be explained in as much detail as they need to tell me :)

the more detailed, the better. I usually have problems, if barely more than the problem is given.


could you specify which part / file should i paste here?

I think one question block (of popquiz.htm) is sufficient for me.

what I have in mind is something like this:
- make an Object, that holds the correct answers (imagine a hash table or associative array)
- make an Object, that holds the actual answers
- compare both objects and print the result (actually I’d stay on the same page here)

PS. does it have to work in Internet Explorer?

PPS. does the answers have to be concealed to the participants? (if you’re out cheating, a look at the page sources reveals the correct answers!)

mayankdb
06-05-2010, 11:59 AM
Dormilich,
many thanks for your patience. here's the representative HTML code from popquiz.htm



var actualchoices=new Array()
document.cookie="ready=yes"
</script>

<form method="POST" name="myquiz">

<font face="Arial"><big><big>General Knowledge Quiz</big></big></font></p>

<div class="qheader">
1) If A can do a piece of work in 10 days while B needs 15 days, how long will both take on the same work, if put together?</div>
<div class="qselections">
<input type="radio" value="a" name="question1">a) 6<br>
<input type="radio" value="b" name="question1">b) 7.5<br>
<input type="radio" value="c" name="question1">c) 9<br>
<input type="radio" value="d" name="question1">d) none of these<br>
</div>

<br>

<div class="qheader">
2) What is the world's most common religion?</div>
<div class="qselections">
<input type="radio" value="a" name="question2">a) Christianity<br>
<input type="radio" value="b" name="question2">b) Buddhism<br>
<input type="radio" value="c" name="question2">c) Hinduism<br>
<input type="radio" value="d" name="question2">d) Muslim<br>
</div>

<div align="center">
<input type="button" value="Grade Me!" name="B1" onClick="gradeit()"> <input type="button" value="Reset" name="B2" onClick="document.myquiz.reset()"></div>
</form>

</body>
</html>




as to the questions you've posed: (1) yes, i'll prefer it work on IE5+ as IE is more popular around where i work. (2) i don't know if the answers can be visible because they are in the quizconfig.js file, which i believe can't be seen by students taking the test, right? but yes, it'll be very good if students can't cheat.

the result.htm file does the actual processing, and if you could just change the following part - which does not have negative marking for incorrect answers - i think my objective will be achieved and you will have one less novice pestering you. :)


<script>
var wrong=0
for (e=0;e<=2;e++)
document.result[e].value=""

var results=document.cookie.split(";")
for (n=0;n<=results.length-1;n++){
if (results[n].charAt(1)=='q')
parse=n

}

var incorrect=results[parse].split("=")
incorrect=incorrect[1].split("/")
if (incorrect[incorrect.length-1]=='b')
incorrect=""
document.result[0].value=totalquestions-incorrect.length+" out of "+totalquestions
document.result[2].value=(totalquestions-incorrect.length)/totalquestions*100+"%"
for (temp=0;temp<incorrect.length;temp++)
document.result[1].value+=incorrect[temp]+", "


i suppose it's the colored part one would need to tinker a bit to achieve the negative marking bit?
thanks

-mdb

Sciliano
06-05-2010, 01:37 PM
See:

http://codingforums.com/showthread.php?t=103688&highlight=Quiz

Dormilich
06-06-2010, 11:54 AM
as to the questions you've posed: (1) yes, i'll prefer it work on IE5+ as IE is more popular around where i work.

OMG, I thought IE5 would be dead.

IE5 will give trouble, since it’s JavaScript engine is … let’s talk about something different.

another problem I have with IE is, that I don’t have one, so I can’t test code against it (although my guessing is not bad, when it comes to the recent versions).


(2) i don't know if the answers can be visible because they are in the quizconfig.js file, which i believe can't be seen by students taking the test, right? but yes, it'll be very good if students can't cheat.

not right. everything you send to the browser, can be seen, including .js files.


See: http://codingforums.com/showthread.php?t=103688&highlight=Quiz
looks promising

mayankdb
06-06-2010, 01:58 PM
Dormilich

Thank you for taking the time.

Ha ha, IE should have been dead, but apparently i live in the yester-generation.

Anyway, if you could please look into how to give negative marks for incorrect answers, i suppose it would solve most of my problems.

Also, is there a command that would make this happen: If I click a particular radio button(s) in a file, say FileA.htm, a corresponding radio button(s) would be automatically clicked in, say, FileB.htm?

-mdb

Dormilich
06-09-2010, 03:34 AM
Anyway, if you could please look into how to give negative marks for incorrect answers, i suppose it would solve most of my problems.

if you want to make your script secure (against cheating), you have to use a server side language, and due to the IE5 issue, you canít use AJAX (afaik not supported in IE5)


Also, is there a command that would make this happen: If I click a particular radio button(s) in a file, say FileA.htm, a corresponding radio button(s) would be automatically clicked in, say, FileB.htm?

no. because you only have one page active at a time.

Dormilich
06-09-2010, 04:12 AM
I threw some code together

// changes in the HTML
<input type="button" value="Grade Me!" id="B1">

// correct answers
var solutions = {
question1: "d",
question2: "c" // not sure about that one
}
// collect current answers
function getAnswers()
{
var i,
answers = {}, // will be similar to the solutions variable
inp = document.getElementsByTagName("input");

for (i = inp.length; i--;) {
if (inp[i].checked) { // collect the checked ones
answers[inp[i].name] = inp[i].value;
}
}
return answers;
}
function compare(answ, sol)
{
var prop,
right = 0,
wrong = 0,
omitted = 0,
score;

for (prop in sol) {
// some rough filtering, not too foolproof. but itís IE, after all
if (typeof sol[prop] == "function") {
continue;
}
// compare the answers and solutions object
if (prop in answ) {
if (answ[prop] == sol[prop]) {
right++;
}
else {
wrong++;
}
}
else {
omitted++;
}
}
// score it
score = Math.round(100 * right / (right + wrong + omitted));
// a fast result display
alert("you have answered " + right + " questions correctly, " + wrong + " incorrectly and " + omitted + " not at all. you earned a score of " + score + "%.")
}
// event handler
function grade()
{
compare(getAnswers(), solutions);
}
document.getElementById("B1").onclick = grade;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum