Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp me fix my JavaScript quiz
    primarykey
    data
    text
    <p>I have been at this quiz for ages now and just can't get it to work on my webpage. What is the error?</p> <p>The HTML:</p> <pre><code>&lt;body&gt; &lt;div id="content1"&gt; &lt;div class="position"&gt; &lt;div class="form"&gt; &lt;h3&gt;Quiz&lt;/h3&gt; &lt;form name="myForm"&gt; Question 1: Which of these is a letter?&lt;br&gt; &lt;input type="radio" name="q1" value="correct"&gt;A&lt;br&gt; &lt;input type="radio" name="q1"&gt;1&lt;br&gt; &lt;input type="radio" name="q1"&gt;#&lt;br&gt; &lt;input type="radio" name="q1"&gt;None of the Above&lt;br&gt; &lt;br&gt; Question 2: Which of these is a number?&lt;br&gt; &lt;input type="radio" name="q2"&gt;A&lt;br&gt; &lt;input type="radio" name="q2" value="correct"&gt;1&lt;br&gt; &lt;input type="radio" name="q2"&gt;#&lt;br&gt; &lt;input type="radio" name="q2"&gt;None of the Above&lt;br&gt; &lt;br&gt; etc... &lt;input name="button" type="Submit" onClick="onclick=return checkAnswers()" /&gt; &lt;/form&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p>The JavaScript code:</p> <pre><code>&lt;head&gt; &lt;script language="JavaScript"&gt; //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 &lt; temp.length; j++) { //If the correct one is chosen then add 1 to total score. if (temp[j].value == "correct" &amp;&amp; temp[j].checked == true) { totalScore++; } } } //If the total percentage is more than 75%. if ((totalScore/allQuestions.length) &gt;= .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; } } &lt;/script&gt; &lt;/head&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload