www.webdeveloper.com
Results 1 to 4 of 4

Thread: Submit answer for quiz

Hybrid View

  1. #1
    Join Date
    Nov 2011
    Posts
    64

    Submit answer for quiz

    Code:
    <html>
    <body>
    <form name="myForm">
     Question 1: Which of these is a letter?<br>
     <input type="radio" name="q1" value="correct">A<br>
     <input type="radio" name="q1">1<br>
     <input type="radio" name="q1">#<br>
     <input type="radio" name="q1">None of the Above<br>
     <br>
     <br>
     Question 2: Which of these is a number?<br>
     <input type="radio" name="q2">A<br>
     <input type="radio" name="q2" value="correct">1<br>
     <input type="radio" name="q2">#<br>
     <input type="radio" name="q2">None of the Above<br>
     <br>
     <br>
     Question 3: Which of these is a symbol?<br>
     <input type="radio" name="q3">A<br>
     <input type="radio" name="q3">1<br>
     <input type="radio" name="q3" value="correct">#<br>
     <input type="radio" name="q3">None of the Above<br>
     <br>
     <br>
     Question 4: Which of these is a comma?<br>
     <input type="radio" name="q4">A<br>
     <input type="radio" name="q4">1<br>
     <input type="radio" name="q4">#<br>
     <input type="radio" name="q4" value="correct">None of the Above<br>
     <br>
     <br>
     
     <a href="#" onclick="return checkAnswers()">Get Certificate</a>
     
    </form>
    </body>
    
    </html>
    <script language="JavaScript">
    
    var allQuestions = new Array(document.myForm.q1,
                                document.myForm.q2,
                                document.myForm.q3,
                                document.myForm.q4);
    
    
    function checkAnswers(){
     
    
    var totalScore = 0;
    
      
       for (var i = 0; i < allQuestions.length; i++) {
    
         if (allQuestions[i].value == "correct" && allQuestions[i].checked == true) {
           totalScore++;
        
       }
    
     }
    
     if ((totalScore/allQuestions.length) >= .75) {
    
       alert("Congratulations! Your score of " + totalScore +
             " out of " + allQuestions.length + " is good enough to proceed!");
    
       window.location.href = "certificate.html";
     }
     else{
       
       alert("You must get at least 75% correct to move on!");
       return false;
     }
    }
    </script>
    I think i have some problems with var totalScore. The code don't process on to certification.html page. Can someone help ?

  2. #2
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    certification.html or certificate.html?

  3. #3
    Join Date
    Nov 2011
    Posts
    64
    certificate.html, I think i have problem with the for loop. It doesn't add the totalScore++;

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,672
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <form name="myForm">
     Question 1: Which of these is a letter?<br>
     <input type="radio" name="q1" value="correct">A<br>
     <input type="radio" name="q1">1<br>
     <input type="radio" name="q1">#<br>
     <input type="radio" name="q1">None of the Above<br>
     <br>
     <br>
     Question 2: Which of these is a number?<br>
     <input type="radio" name="q2">A<br>
     <input type="radio" name="q2" value="correct">1<br>
     <input type="radio" name="q2">#<br>
     <input type="radio" name="q2">None of the Above<br>
     <br>
     <br>
     Question 3: Which of these is a symbol?<br>
     <input type="radio" name="q3">A<br>
     <input type="radio" name="q3">1<br>
     <input type="radio" name="q3" value="correct">#<br>
     <input type="radio" name="q3">None of the Above<br>
     <br>
     <br>
     Question 4: Which of these is a comma?<br>
     <input type="radio" name="q4">A<br>
     <input type="radio" name="q4">1<br>
     <input type="radio" name="q4">#<br>
     <input type="radio" name="q4" value="correct">None of the Above<br>
     <br>
     <br>
     
     <a href="#" onclick="return checkAnswers()">Get Certificate</a>
     
    </form>
    
    <script language="JavaScript">
    function checkAnswers(){
    var totalScore = 0;
    var elems = document.forms['myForm'].elements;
    var correct_answers = 0;
    for(var i = 0; i < elems.length; i++){
    if(elems[i].value == "correct"){correct_answers++;}
    if(elems[i].value == "correct" && elems[i].checked == true){totalScore++;}
    }
    if((totalScore*100/correct_answers) >= 75){
    alert("Congratulations! Your score of " + totalScore +" out of " + correct_answers + " is good enough to proceed!");
    location.href = "certificate.html";
    }
    else{alert("You must get at least 75&#37; correct to move on!");return false;}
    }
    </script>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles