Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    need help with quiz script

    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....
    Code:
    
    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

  • #2
    New to the CF scene
    Join Date
    Jun 2010
    Location
    melton24,new york,NY 10002 U.S.A.
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re:

    i just now read this post this is very nice post i like this post and very influence with this post
    Advanced colon max

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    mayankdb (06-04-2010)

  • #4
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by mayankdb View Post
    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.

    Quote Originally Posted by mayankdb View Post
    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.

    Quote Originally Posted by mayankdb View Post
    could you specify which part / file should i paste here?
    I think one question block (of popquiz.htm) is sufficient for me.

    Edit: 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!)
    Last edited by Dormilich; 06-04-2010 at 12:04 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Dormilich,
    many thanks for your patience. here's the representative HTML code from popquiz.htm

    Code:
    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.
    Code:
    <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

  • #7
    Regular Coder
    Join Date
    Nov 2009
    Posts
    247
    Thanks
    4
    Thanked 22 Times in 22 Posts

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by mayankdb View Post
    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).

    Quote Originally Posted by mayankdb View Post
    (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.

    Quote Originally Posted by Sciliano View Post
    looks promising
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    quiz

    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

  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by mayankdb View Post
    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)

    Quote Originally Posted by mayankdb View Post
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #11
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    I threw some code together
    PHP Code:
    // changes in the HTML
    <input type="button" value="Grade Me!" id="B1"
    PHP Code:
    // 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 (
    inp.lengthi--;) {
            if (
    inp[i].checked) { // collect the checked ones
                
    answers[inp[i].name] = inp[i].value;
            }
        }
        return 
    answers;
    }
    function 
    compare(answsol)
    {
        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
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •