title: How can I select radio button values through for loop in JavaScript?
body: <p>I'm writing a form for a personality test. The page has questions where the user would need to choose an answer for each. The page will not allow the user to skip questions and it will alert them to answer the missing questions. When the form is submitted, the page will show the score.</p>
  
  <p>I'm trying to store the values of radio buttons in an array using a for loop. It is not working.</p>
  
  <p>Here is the HTML code:</p>
  
  <pre><code>&lt;!-- Form --&gt;
  &lt;form name="quiz" onsubmit="return show()"&gt;
  
      &lt;!-- Question 1 --&gt;
      &lt;p&gt;Question 1&lt;/p&gt;
      &lt;input type="radio" name="q0" value="a"&gt; a
      &lt;br&gt;
      &lt;input type="radio" name="q0" value="b"&gt; b
      &lt;br&gt;
      &lt;input type="radio" name="q0" value="c"&gt; c
  
      &lt;!-- Question 2 --&gt;
  
      &lt;!-- Question 3 --&gt;
  
      &lt;!-- Question 4 --&gt;
  
      &lt;!-- Question 5 --&gt;
      &lt;p&gt;Question 5&lt;/p&gt;
      &lt;input type="radio" name="q4" value="a"&gt; a
      &lt;br&gt;
      &lt;input type="radio" name="q4" value="b"&gt; b
      &lt;br&gt;
      &lt;input type="radio" name="q4" value="c"&gt; c
  
      &lt;!-- Submit Button --&gt;
      &lt;input type="submit" value="Show Result"&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>Here is my JavaScript code:</p>
  
  <pre><code>function show() {
  
      var questions = 5;
      var score = 0;
      var solution = ["a", "c", "a", "b", "b"];
      var answers = [];
  
      for (i = 0; i &lt; questions; i++) {
  
          answers[i] = document.quiz.eval('q' + i).value;
  
          if (answers[i] == null || answers[i] == '') {
              window.alert("You need to answer question " + (i + 1));
              return false;
          }
  
          if (solution[i] == answers[i]) {
              score++;
          }
  
      }
      window.alert(score);
  }
  </code></pre>