SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Location
    Iceland
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I think I need help with a conditional function.

    Hi
    I appologise in advance that I know nothing about javascript
    The following is a snippet of code that generates right or wrong answers from an online quiz. The script is in the header of this page.

    Basically, what I want is a function that will give the visitor a link to the next quiz, BUT ONLY if they get 100% or 10/10. Would it require a new line? would it call the conditions of the +right+ function? I have been trying to add a function in the second to last table cell where it generates the +right+

    Any help on this would be much appreciated.

    Code:
    <script language='JavaScript'>
    <!--
    for(i=0;i<qnum;i++){
    	for(a=0;a<4;a++){
    		var rbid='rb'+i+'_'+a;
    		if(document.getElementById(rbid).value=='1'&&document.getElementById(rbid).checked==true){
    			ghtml+='<table width=\"100%\"><tr><td style=\"border:1 solid black;background-color:grey\"><span style=\"color:green\"><b>Correct</b></span></td></tr><tr><td style=\"border:1 solid black;background-color:#grey\"><b>'+eval(i+1)+'. '+qarray[i]+'</b><p><table width=\"100%\"><tr><td width=\"50%\"><i><span style=\"color:green\">Your Answer</span>:</i> '+answerarray[i][a]+'</td><td width=\"50%\"><i><span style=\"color:green\">Correct Answer</span>:</i> '+answerarray[i][canswerarray[i]]+'</td></tr></table></td></tr></table><p>&nbsp;';
    			right++;		}
    		if(document.getElementById(rbid).value=='0'&&document.getElementById(rbid).checked==true){
    			ghtml+='<table width=\"100%\"><tr><td style=\"border:1 solid black;background-color:grey\"><span style=\"color:red\"><b>InCorrect</b></span></td></tr><tr><td style=\"border:1 solid black;background-color:grey\"><b>'+eval(i+1)+'. '+qarray[i]+'</b><p><table width=\"100%\"><tr><td width=\"50%\"><i><span style=\"color:red\">Your Answer</span>:</i> '+answerarray[i][a]+'</td><td width=\"50%\"><i><span style=\"color:green\">Correct Answer</span>:</i> '+answerarray[i][canswerarray[i]]+'</td></tr></table></td></tr></table><p>&nbsp;';
    			wrong++;		}
    	}
    }
    if(gmethod==0){ghtml+='<div align=\"center\"><table width=\"100%\" bgcolor=\"grey\"><tr><td width=\"100%\"><span style=\"font-size:14pt\"><b>Grade</b></span></td></tr><tr><td width=\"100%\"><table width=\"100%\"><tr><td width=\"50%\"><b><u>Correctly Answered Questions</u>:</b> '+right+'</td><td width=\"50%\"><b><u>InCorrectly Answered Questions</u>:</b> '+wrong+'</td></tr></table></td></tr></table>';}
    if(gmethod==1){
    	grade=100/qnum;
    	right=grade*right;
    	wrong=grade*wrong;
    	ghtml+='<div align=\"center\"><table width=\"100%\" bgcolor=\"grey\"><tr><td width=\"100%\"><span style=\"font-size:14pt\"><b>Grade</b></span></td></tr><tr><td width=\"100%\"><table width=\"100%\"><tr><td width=\"50%\"><b><u>Correctly Answered Questions</u>:</b> '+right+'%</td><td width=\"50%\"><b><u>InCorrectly Answered Questions</u>:</b> '+wrong+'%</td></tr></table></td></tr></table>';}
    ghtml+='<p align=\"center\"><input type=\"button\" value=\"Reset\" onClick=\"location.reload()\"/>';
    document.getElementById('qview').style.display='none';
    document.getElementById('gradeview').style.display='inline';
    document.getElementById('gradeview').innerHTML=ghtml;
    }//-->
    </script>

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,329
    Mentioned
    191 Post(s)
    Tagged
    4 Thread(s)
    It appears the variable "right" contains the number of correct answers.
    You could do something like this:

    <script language="javascript">
    <!--
    if(wrong==0) document.write("<a href=\"yourlinkgoeshere.com\">Next Quiz</a>\n";
    -->
    </script>

    Hope this helps.
    Off Topic:


    Testing by way of a client-side technology, like Javascript, is generally a bad idea.

    For example:
    I know nothing of the subject matter but, with a View Source, I was able to get these correct answers:
    var canswerarray=new Array()
    canswerarray[0]='2';
    canswerarray[1]='1';
    canswerarray[2]='3';
    canswerarray[3]='1';
    canswerarray[4]='2';
    canswerarray[5]='0';
    canswerarray[6]='2';
    canswerarray[7]='2';
    canswerarray[8]='2';
    canswerarray[9]='3';

    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Member
    Join Date
    Jan 2007
    Location
    Iceland
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks for your help.
    Yes I am aware of the problems with using a java script for testing, but in the end the cheater only hurts themselves.

  4. #4
    SitePoint Member
    Join Date
    Jan 2007
    Location
    Iceland
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wonder if I could get some more help as I am using a different script and I couldn't get the function (above) to work with it.
    Here is the new script:
    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="easy1results.htm"
    }
    
    
    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()
    }
    Many thanks for any help!
    The url for the quiz
    burnmybanjo.com it's no secret!


Bookmarks

Posting Permissions

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