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

    Hi I need Help :(

    Hi
    PLZ HELP ME
    this code is for randomly showing questions of a quize, but it's not working??!!!
    Code:
    <html> <head> <script>
    
    
    function DisplayQuestions() {
       var MCQ = new Array(5);
       MCQ[0] =document.getElementById("Q1");
       MCQ[1] =document.getElementById( "Q2");
       MCQ[2] =document.getElementById( "Q3");
       MCQ[3] =document.getElementById( "Q4");
       MCQ[4] = document.getElementById("Q5");
       
       var i = 0;
    
       for(i=0;i<5;i++) {
          var Q = Math.floor(1+Math.random()*5);
              
          if (MCQ[Q] !=null ) {
    	 document.writeln("Question number " + Q + " is " + MCQ[Q]);
    	 i++;}
    	 MCQ[Q] = null; // Dump this question - it's been displayed
          
       }
    }
    
    
    function CheckAns(){
     var myQuiz = document.getElementById( "Quiz" );
    var g=0;
                // determine whether the answer is correct
                if ( Quiz.elements[ 2 ].checked )
                    g++; 
    }     
    
    </script>
    </head>
    <body>
     <form id = "Quiz" onsubmit = "CheckAns()" action = "">
    <p id="Q1"> What is the only function all C++ programs must contain? <br/>
    
    <input  type="radio" name="c" value="A"/> <label>A.start()</label>   <br/>
    <input  type="radio" name="c" value="B"/><label>B.system()</label>  <br/>
    <input  type="radio" name="c" value="C"/>    <label>C.main()</label><br/>
    <input  type="radio" name="c" value="D"/> <label>D.program()</label><br/> 
     <input type = "submit" name = "submit" value = "Submit" />
                <input type = "reset" name = "reset" value = "Reset" />
             </p>
          
    
    
    
    <p id="Q2"> What punctuation is used to signal the beginning and end of code blocks? <br/>
    
    <input  type="radio" name="c" value="A"/> <label>A.{ }</label>   <br/>
    <input  type="radio" name="c" value="B"/><label>B.-> and <-</label>  <br/>
    <input  type="radio" name="c" value="C"/>    <label>C.BEGIN and END</label><br/>
    <input  type="radio" name="c" value="D"/> <label>D.(and)</label><br/> 
     <input type = "submit" name = "submit" value = "Submit" />
                <input type = "reset" name = "reset" value = "Reset" />
             </p>
    
    
    
    <p id="Q3"> Which of the following is a correct comment? <br/>
    
    <input  type="radio" name="c" value="A"/> <label>A.*/ Comments */</label>   <br/>
    <input  type="radio" name="c" value="B"/><label>B.** Comment **</label>  <br/>
    <input  type="radio" name="c" value="C"/>    <label>C./* Comment */</label><br/>
    <input  type="radio" name="c" value="D"/> <label>D.{ Comment }</label><br/> 
     <input type = "submit" name = "submit" value = "Submit" />
                <input type = "reset" name = "reset" value = "Reset" />
             </p>
    
    
    
    </form>
    
    </body></html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    var Q = Math.floor(1+Math.random()*5);

    Your questions are numbered starting at *ZERO* so you do *NOT* want to add 1 to the randomly generated value.

    var Q = Math.floor(Math.random()*5);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    You have other problems, too. But that will get you started.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.

  • #5
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Now How can i find the score

    Code:
    <html> <head> <script>
    
    var score=new Array();
    
     var MCQ =new Array ("What is the only function all C++ programs must contain?<br/>", 
    "What punctuation is used to signal the beginning and end of code blocks?<br/>", 
    "What punctuation ends most lines of C++ code?<br/>",
     "Which of the following is the correct operator to compare two variables?<br/>", 
    "Which of the following is not a correct variable type?<br/>"); 
    
    
    var Q1=new Array("<form id=\"q1\"><input type=\"radio\" name=\"A\" value=\"A\"/> <label>start()</label><br/>",
    "<input  type=\"radio\" name=\"B\" value=\"B\"/><label>system()</label>  <br/>",
    "<input  type=\"radio\" name=\"C\" value=\"C\" onClick=score[0]=1 />    <label>main()</label><br/>",
    "<input  type=\"radio\" name=\"D\" value=\"D\"/> <label>program()</label></form><br/> <br/>");
    
    var Q2=new Array("<input  type=\"radio\" name=\"c\" value=\"A\"  onClick=\"score[1]=1\"/> <label>{ }</label><br/>",
    "<input  type=\"radio\" name=\"c\" value=\"B\"/><label>-> and <-</label>",
    "<br/><input  type=\"radio\" name=\"c\" value=\"C\"/> <label>BEGIN and END</label><br/>",
    "<input  type=\"radio\" name=\"c\" value=\"D\"/> <label>(and)</label> <br/> <br/>"); 
    
    
    
    
    var Q3=new Array("<input  type=\"radio\" name=\"c\" value=\"A\"/> <label>(dot)</label><br/>",
    "<input  type=\"radio\" name=\"c\" value=\"B\"  onClick=\"score[2]=1\" /><label>;(Semi colon)</label>",
    "<br/><input  type=\"radio\" name=\"c\" value=\"C\"/> <label>:(Colon)</label><br/>",
    "<input  type=\"radio\" name=\"c\" value=\"D\"/> <label>'(Single quote)</label> <br/> <br/>"); 
    
    
    
    var Q4=new Array("<input  type=\"radio\" name=\"c\" value=\"A\"/> <label>:=</label><br/>",
    "<input  type=\"radio\" name=\"c\" value=\"B\"/><label>=</label>",
    "<br/><input  type=\"radio\" name=\"c\" value=\"C\"/> <label>Equal</label><br/>",
    "<input  type=\"radio\" name=\"c\" value=\"D\"  onClick=\"score[3]=1\"/> <label>==</label> <br/> <br/>"); 
    
    
    
      
    var Q5=new Array("<input  type=\"radio\" name=\"c\" value=\"A\"/> <label>float</label><br/>",
    "<input  type=\"radio\" name=\"c\" value=\"B\"  onClick=\"score[4]=1\"/><label>real</label>",
    "<br/><input  type=\"radio\" name=\"c\" value=\"C\"/> <label>int</label><br/>",
    "<input  type=\"radio\" name=\"c\" value=\"D\"/> <label>double</label> <br/> <br/>"); 
    
    
    
    
    
    
    
    
    
    function DisplayQuestions() {
       
    
       dis: for(var i=0;i<100;i++) {
                var Q = Math.floor(Math.random()*5);
                   
                    if(MCQ[Q]==null)
                    continue dis; 
    	         else {
                      
                      document.writeln( MCQ[Q]);
                      
                      if(Q==0){
               
               ans1:  for(var j=0;j<=100;j++){
                      var r = Math.floor(Math.random()*4);
                      if(Q1[r]==null)
                      continue ans1; 
                      else  {document.writeln(Q1[r]);
                      Q1[r]=null;} document.write("<br/>") }
                      }
            
             else     if(Q==1){
                     ans2: for(var a=0;a<=100;a++){
                      r = Math.floor(Math.random()*4);
                      if(Q2[r]==null)
                      continue ans2; 
                      else {document.writeln(Q2[r]);
                      Q2[r]=null;} document.write("<br/>");}
                      }//end if
    
    
    
                     if(Q==2){
                     ans3: for(var a=0;a<=100;a++){
                      r = Math.floor(Math.random()*4);
                      if(Q3[r]==null)
                      continue ans3; 
                      else {document.writeln(Q3[r]);
                      Q3[r]=null;} document.write("<br/>");}
                      }//end if
    
    
    
    if(Q==3){
                     ans4: for(var a=0;a<=100;a++){
                      r = Math.floor(Math.random()*4);
                      if(Q4[r]==null)
                      continue ans4; 
                      else {document.writeln(Q4[r]);
                      Q4[r]=null;} document.write("<br/>");}
                      }//end if
    
    
    
    
    if(Q==4){
                     ans5: for(var a=0;a<=100;a++){
                      r = Math.floor(Math.random()*4);
                      if(Q5[r]==null)
                      continue ans5; 
                      else {document.writeln(Q5[r]);
                      Q5[r]=null;} document.write("<br/>");}
                      }//end if
    
                      } MCQ[Q]=null;  
     
         document.write("<br/>"); }}
    
    DisplayQuestions()
    
    
    
    function CheckAns(){
    
    for(var i=0;i<5;i++)
    var total=total+score[i];
    document.write("the total is= "+total);
    }     
    
    </script>
    </head>
    <body>
    
    
    
    
    
    
    <input type = "submit" name = "submit" value = "Submit" onClick="CheckAns()" /> <input type = "reset" name = "reset" value = "Reset" 
    </body></html>

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Lots and lots of problems in that code.

    The funniest one is maybe this one: If the user clicks on every single radio button (all possible ones) on the page, he is guaranteed a perfect score.

    You should *NOT* keep score "on the fly" as the buttons are pushed.

    Only when the user finally pushes a "complete" button should you go calculate score.

    Of course, nobody would use code like this in real life, as the user can simply view the code on the page to find all the correct answers.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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