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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScriptKit.com Multiple Choice Quiz - Question

    Love the script - highly flexible, really easy to use for someone like me who is farily new to Javascript....having one issue though....

    I have 14 questions in my quiz and when the Grade appears in percentage format, and I answer 1 question correct - my grade is 7.142857142857142%

    Is there any way to shorten this to just a single digit?

    Plus - does the script work in safari on a mac? Any way to make this work?

    Thanks so much

    Stewart

    Code for js is as follows:

    /***********************************************
    * JavaScriptKit.com Multiple Choice Quiz Script (http://www.javascriptkit.com)
    * Copyright 2003 JavaScript Kit- http://www.javascriptkit.com
    * This notice and footnote must stay intact for use
    * Visit JavaScript Kit (http://www.javascriptkit.com/) for full source code
    ***********************************************/

    //Enter total number of questions:
    var totalquestions=14

    //Enter the solutions corresponding to each question:
    var correctchoices=new Array()
    correctchoices[1]='b' //question 1 solution
    correctchoices[2]='a' //question 2 solution
    correctchoices[3]='c' //question 3 solution
    correctchoices[4]='a' //question 4 solution
    correctchoices[5]='b' //question 5 solution
    correctchoices[6]='b' //question 6 solution
    correctchoices[7]='a' //question 7 solution
    correctchoices[8]='b' //question 8 solution
    correctchoices[9]='b' //question 9 solution
    correctchoices[10]='a' //question 10 solution
    correctchoices[11]='c' //question 11 solution
    correctchoices[12]='c' //question 12 solution
    correctchoices[13]='a' //question 13 solution
    correctchoices[14]='a' //question 14 solution

    /////Don't edit beyond here//////////////////////////

    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.html"
    }


    function showsolution(){
    var win2=window.open("","win2","width=200,height=350, scrollbars")
    win2.focus()
    win2.document.open()
    win2.document.write('<title>Solution</title>')
    win2.document.write('<body bgcolor="#FFFFFF">')
    win2.document.write('<center><h3>Solution to Quiz</h3></center>')
    win2.document.write('<center><font face="Arial">')
    for (i=1;i<=totalquestions;i++){
    for (temp=0;temp<incorrect.length;temp++){
    if (i==incorrect[temp])
    wrong=1
    }
    if (wrong==1){
    win2.document.write("Question "+i+"="+correctchoices[i].fontcolor("red")+"<br>")
    wrong=0
    }
    else
    win2.document.write("Question "+i+"="+correctchoices[i]+"<br>")
    }
    win2.document.write('</center></font>')
    win2.document.write("<h5>Note: The solutions in red are the ones to the questions you had incorrectly answered.</h5><p align='center'><small><a href='http://www.javascriptkit.com' target='_new'>JavaScript Kit quiz script</a></small>")
    win2.document.close()
    }

  • #2
    Regular Coder
    Join Date
    Jul 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Replace the following line in results.htm
    Code:
    document.result[2].value=(totalquestions-incorrect.length)/totalquestions*100+"%"
    with this
    Code:
    document.result[2].value=((totalquestions-incorrect.length)/totalquestions*100).toFixed(2)+"%"

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks!

    mongus - thanks so much for your help. Makes perfect sense.

    Stewart


  •  

    Posting Permissions

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