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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Return to beginning of function.

    Hello all,
    I have a multiple choice quiz that works fairly well. However, instead of it presenting "End of Quiz" message when last question is completed I need it to take the user back to the beginning (question 1) but with no indication to the user that this has been done.
    How can I adjust the code to have it do this?
    (thanking you in anticipation)

    Code:
    <script language="JavaScript1.1">
    var whichone=1
    var tempmn=document.quiz.thequestion
    
    function generatequestions(){
    document.quiz.theresponse.selectedIndex=0
    if (!document.quiz.cmode.checked||whichone>=total+1||whichone<=0)
    document.quiz.thesolution.value=''
    if (whichone>=total+1) 
    tempmn.value="End of quiz"
    else{
    tempmn.value=whichone+")"+question[whichone]+"\n\n"+"a)"+eval('choice'+whichone+'[1]')+"\n"+"b)"+eval('choice'+whichone+'[2]')+"\n"+"c)"+eval('choice'+whichone+'[3]')+"\n"+"d)"+eval('choice'+whichone+'[4]')
    if (document.quiz.cmode.checked)
    document.quiz.thesolution.value=solution[whichone]
    }
    }
    function responses(){
    var temp2=document.quiz.theresponse
    var temp3=temp2.options[temp2.selectedIndex].text
    if (temp3!=solution[whichone]&&temp2.selectedIndex!=0)
    document.quiz.thesolution.value="Sorry, the correct answer is "+solution[whichone]
    else if(temp2.selectedIndex!=0)
    document.quiz.thesolution.value=compliments[Math.round(Math.random()*compliments.length-1)]
    }
    generatequestions()
    </script>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    *IF* I understand what you are saying--and *IF* I can indent your code meaningfully so I can read it--then I *think* this is what you want:

    Code:
    function generatequestions()
    {
         document.quiz.theresponse.selectedIndex=0
         if ( ! document.quiz.cmode.checked || whichone >= total+1 || whichone<=0 )
         {
             document.quiz.thesolution.value='';
         }
         if (whichone>=total+1) whichone = 1;
     
         tempmn.value = whichone + ")" + question[whichone] +"\n\n"
               + "a)" + eval('choice'+whichone + '[1]') + "\n" 
               + "b)" + eval('choice'+whichone + '[2]') + "\n"  
               + "c)" + eval('choice'+whichone + '[3]') + "\n" 
               + "d)" + eval('choice'+whichone + '[4]');
        if (document.quiz.cmode.checked)
            document.quiz.thesolution.value=solution[whichone];
    }
    I'm not at all sure what the purpose of your line in blue there is.

    But the line in red will, if I understand your code, simply set the question counter back to 1 when it gets too high.

    Is that what you meant??

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Return to beginning of function - 2 clicks?

    Hello Old Pedant.
    Thats exactly what I meant, thankyou very much.
    I adjusted the code as you described and it worked except for a small glitch. Having completed the last question the next click should take us back to the beginning of the quiz - it took two clicks to do that it then presented the second question not the first.
    I adjusted the code by changing 1 to 0 (see red coloured code) and this presented question 1 OK -

    Code:
    if (whichone>=total+1) whichone = 0;
    - however, it still takes two clicks to get us there. Can you see where this can be fixed?
    Code:
    <div align="left">
    <table border="0" cellspacing="0" cellpadding="0" width="98%">
    <!-- FOLLOWING SCRIPT IS TO EXTRACT QUESTION FROM JAVASCTIPFILE AND PLACE IN HTML -->
      <tr>
        <td><form method="POST" name="instantquiz">
          <table border="0" width="100%" cellspacing="0" cellpadding="0">
            <tr>
              <td><textarea rows="15" name="thequestion" cols="50"
              wrap="virtual"></textarea></td>
            </tr>
            <tr align="left">
              <td>
    		  <strong>Choose:</strong> <select
              name="theresponse" size="1" onChange="responses()">
                <option value="-----">----</option>
                <option value="a">a</option>
                <option value="b">b</option>
                <option value="c">c</option>
                <option value="d">d</option>
              </select> 
    		  <strong>Result: </strong><input
              type="text" name="thesolution" size="35"></td>
            </tr>
            <tr align="left">
              <td><input type="button" value="&lt;&lt;Back" name="B1"
              onClick="if (whichone&gt;1){whichone--;generatequestions()}"> <input type="button"
              value="Next&gt;&gt;" name="B2"
              onClick="if (whichone&lt;=total){whichone++;generatequestions()}">
              <small><a href="#" onClick="whichone=1;generatequestions();return false">Start Over</a> </small><input
              type="checkbox" name="cmode" value="ON"><small>Cheat Mode</small></td>
            </tr>
          </table>
        </form>
        </td>
      </tr>
    </table>
    If you need to see any more of the code let me know.
    PS I didn't know what the blue code was doing there either so I removed it Thanks again for your time and support.

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've just noticed that the first click clears the 'Result' area then the second click returns us to question 1.
    Hope this is usefull.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Since you didn't really give me enough code to really check out your system, I faked it. I got rid of your eval( ) and your question array and just dumped out text to the textarea, for checking purposes.

    This works for me. Dunno why you saw odd stuff you did.
    Code:
    <html><head><script>
    var whichone=1
    var total = 5;
    
    function generatequestions(bump)
    {
         whichone += bump;
         if ( whichone < 1 ) whichone = total;
         if ( whichone > total ) whichone = 1;
    
         var tempmn=document.quiz.thequestion
         document.quiz.theresponse.selectedIndex=0
         if ( ! document.quiz.cmode.checked || whichone >= total+1 || whichone<=0 )
         {
             document.quiz.thesolution.value='';
         }
     
         tempmn.value = whichone + ")" + "question[" + whichone + "]\n\n"
               + "a)" + ('choice'+whichone + '[1]') + "\n" 
               + "b)" + ('choice'+whichone + '[2]') + "\n"  
               + "c)" + ('choice'+whichone + '[3]') + "\n" 
               + "d)" + ('choice'+whichone + '[4]');
        if (document.quiz.cmode.checked)
            document.quiz.thesolution.value= "solution[" + whichone + "]";
    }
    </script>
    </head>
    <body onload="generatequestions(0);">
    
    <div align="left">
    <table border="0" cellspacing="0" cellpadding="0" width="98%">
    <!-- FOLLOWING SCRIPT IS TO EXTRACT QUESTION FROM JAVASCTIPFILE AND PLACE IN HTML -->
      <tr>
        <td><form method="POST" name="quiz">
          <table border="0" width="100%" cellspacing="0" cellpadding="0">
            <tr>
              <td><textarea rows="15" name="thequestion" cols="50"
              wrap="virtual"></textarea></td>
            </tr>
            <tr align="left">
              <td>
    		  <strong>Choose:</strong> <select
              name="theresponse" size="1" onChange="responses()">
                <option value="-----">----</option>
                <option value="a">a</option>
                <option value="b">b</option>
                <option value="c">c</option>
                <option value="d">d</option>
              </select> 
    		  <strong>Result: </strong><input
              type="text" name="thesolution" size="35"></td>
            </tr>
            <tr align="left">
              <td><input type="button" value="&lt;&lt;Back" name="B1"
              onClick="generatequestions(-1);"> <input type="button"
              value="Next&gt;&gt;" name="B2"
              onClick="generatequestions(1);">
              <small><a href="#" onClick="whichone=1;generatequestions();return false">Start Over</a> </small>
    	<input type="checkbox" name="cmode" value="ON"><small>Cheat Mode</small></td>
            </tr>
          </table>
        </form>
        </td>
      </tr>
    </table>
    </div>
    </body></html>

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    By the by...there is something fundamentally wrong with your data structure (that you didn't show us) that you would *need* to use eval( ).

    No, wrong is too strong a word. Say "incomplete" perhaps?

    I also think you could have done a better job in the presentation, using actual checkboxes along side the answers, instead of dumping the stuff into a textarea.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    I know, I know...I should be shot.

    But look at this example of what I mean. Not prettied up, yet. No styles. But...
    Code:
    <html><head>
    <script type="text/javascript">
    function question(q,a1,a2,a3,a4,s)
    {
       this.question = q;
       this.answers = [ a1, a2, a3, a4 ];
       this.solution = s;
    }
    var questions = new Array(
       new question("Who is buried in Grant's tomb?","U S Grant","Ulysses Simpson Grant",
                    "U. S. Grant", "Ulysses S Grant",4),
       new question("What's the capital of Idaho?","Iduno","Twin Falls",
                    "$1.98","Boise",3),
       new question("What is the value of 3+2*7+4?","39","55","3274","21",4),
       new question("Do you like this quiz?","Yuck","Not especially","It's okay","OF COURSE!",4)
       );
    
    var currentQuestion = 0;
    function getQuestion(bump)
    {
         var form = document.quiz;
         form.ans[0].checked = form.ans[1].checked = form.ans[2].checked = form.ans[3].checked = false;
    
         currentQuestion = ( currentQuestion + questions.length + bump ) % questions.length;
         var q = questions[currentQuestion];
         document.getElementById("question").innerHTML 
                 = "(" + (currentQuestion+1) + ") " + q.question;
         for ( var a = 0; a < 4; ++a )
         {
             document.getElementById("aText"+(a+1)).innerHTML = q.answers[a];
         }
         form.thesolution.value = form.cmode.checked ? q.answers[q.solution-1] : "";
    }
    </script>
    </head>
    <body onload="getQuestion(0);">
    <form name="quiz">
    <table border="0" cellspacing="0" cellpadding="0" width="98%">
    <tr>
        <td id="question" colspan=2></td>
    </td>
    <tr>
        <td style="padding-left: 30px;">
            <input type=checkbox name="ans" value="1" id="UA1">
            <label for="UA1" id="aText1"></label>
        </td>
    </tr>
    <tr>
        <td style="padding-left: 30px;">
            <input type=checkbox name="ans" value="2" id="UA2">
            <label for="UA2" id="aText2"></label>
        </td>
    </tr>
    <tr>
        <td style="padding-left: 30px;">
            <input type=checkbox name="ans" value="3" id="UA3">
            <label for="UA3" id="aText3"></label>
        </td>
    </tr>
    <tr>
        <td style="padding-left: 30px;">
            <input type=checkbox name="ans" value="4" id="UA4">
            <label for="UA4" id="aText4"></label>
        </td>
    </tr>
    <tr>
        <td><hr></td>
    </tr>
    <tr>
        <td style="padding-left:30px;">
            <input type=checkbox name="cmode"> Let me cheat
        </td>
    </tr>
    <tr>
        <td>Solution: <input name="thesolution" size="35"></td>
    </tr>
    <tr>
        <td>
            <input type="button" value="&lt;&lt;Back" onClick="getQuestion(-1);">
    	&nbsp;&nbsp;
            <input type="button" value="Next&gt;&gt;" onClick="getQuestion(1);">
    	&nbsp;&nbsp;
            <input type="button" value="RESET" onClick="currentQuestion=0;getQuestion(0);">
        </td>
    </tr>
    </table>
    </form>
    </body></html>

  • Users who have thanked Old Pedant for this post:

    draig_hand (05-06-2009)

  • #8
    New to the CF scene
    Join Date
    May 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hello Old Pedant,
    Would it be ok to post a reply that contains a more detailed explanaton of what I wish to achieve and the web page code? (such as it is)
    Regards.


  •  

    Tags for this Thread

    Posting Permissions

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