开发者

Help me fix my JavaScript quiz [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have been at this quiz for ages now and just can't get it to work on my webpage. What is the error?

The HTML:

<body>
    <div id="content1">
       <div class="position">
           <div class="form">

              <h3>Quiz</h3>
              <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"&g开发者_如何学JAVAt;#<br>
                  <input type="radio" name="q1">None of the Above<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>

                  etc...

                  <input name="button" type="Submit" onClick="onclick=return checkAnswers()" />
              </form>
         </div>
      </div>
    </div>
</body>

The JavaScript code:

<head>
    <script language="JavaScript">
        //Put all the question sets into this array.
         var allQuestions = new Array(document.myForm.q1,
                               document.myForm.q2,
                               document.myForm.q3,
                               document.myForm.q4);

        //Redirects if 75% or greater, returns false otherwise.
        function checkAnswers(){
            var totalScore = 0; //initialize to 0

            //Go through each question set.
            for (var i in allQuestions) {
                var temp = allQuestions[i];

                //Go through each radio button in the current question set.
                for (var j = 0; j < temp.length; j++) {

                    //If the correct one is chosen then add 1 to total score.
                    if (temp[j].value == "correct" && temp[j].checked == true) {
                        totalScore++;
                    }
                }
            }

            //If the total percentage is more than 75%.
            if ((totalScore/allQuestions.length) >= .75) {
                //Alert and move on.
                alert("Congratulations! Your score of " + totalScore +
                     " out of " + allQuestions.length + " is good enough to proceed!");
            else{
                //Otherwise alert and return false.
                alert("You must get at least 75% correct to move on!");
                return false;
            }
        }
    </script>
</head>


You access the form elements before they exist
You missed a curly bracket before the else. I eliminated it.

<script type="text/javascript>
function validate(theForm) {
 //put all the question sets into this array
  var allQuestions = new Array(theForm.q1,
                        theForm.q2 /*,
                        theForm.q3,
                        theForm.q4 */);

  return checkAnswers(allQuestions);
}
 //redirects if 75% or greater, returns false otherwise
function checkAnswers(allQuestions){
  var totalScore = 0; //initialize to 0
  //go through each question set
  for (var i in allQuestions) {
    var temp = allQuestions[i];
  //go through each radio button in the current question set
    for (var j = 0; j < temp.length; j++) {

    //if the correct one is chosen then add 1 to total score
      if (temp[j].value == "correct" && temp[j].checked == true) {
        totalScore++;
      }
    }
  }

  //if the total percentage is more than 75%
  if ((totalScore/allQuestions.length) >= .75) {
  //alert and move on
    alert("Congratulations! Your score of " + totalScore +
     " out of " + allQuestions.length + " is good enough to proceed!");
     return true; // this will submit the form. return false if you do not want to submit at all
  }
  //otherwise alert and return false
  alert("You must get at least 75% correct to move on!");
  return false;
}
</script>



 <h3>Wine Quiz</h3>
        <form name="Quiz" onsubmit="return validate(this)">

.
.
<input type="submit" />
</form>


This is the working solution and comments for anyone who is looking for it!

// JavaScript Document

function validate(theForm) {  

//put the questions into  an array  
  var allQuestions = new Array(theForm.q1,  
                              theForm.q2,  
                              theForm.q3,  
                              theForm.q4);  

  return checkAnswers(allQuestions);  
}

 //checks all answers  
function checkAnswers(allQuestions){  
  var totalScore = 0; //initialize to 0 

  //go through each question set  
  for (var i in allQuestions) {  
    var temp = allQuestions[i];  

  //go through each radio button in the current question set  
    for (var j = 0; j < temp.length; j++) {  

    //if the correct one is chosen then add 1 to total score  
      if (temp[j].value == "correct" && temp[j].checked == true) {  
        totalScore++;  
      }  
    }  
  }  

  //if the total percentage is more than 75%  
  if ((totalScore/allQuestions.length) >= .75) {  

  //alert and move on  
    alert("Congratulations! Your score of " + totalScore +  
     " out of " + allQuestions.length + " is very good!");  
     return true;  
  }  

    //otherwise alert and return false  
    alert("You must get at least 75% correct to pass");  
    return false;  
}  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜