Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Simple Javascript quiz || answers not validating. ugh!

I am writing this super simple javascript quiz for practice. I am frustrated because I think my code is correct, but my code is not working.

My answers are not 'validating' and I have no clue why. My score is still coming out as 0. My score is not score++ when it should..

The problem is in this snippet:

 for (var j = 0; i <= total.length; j++) {
    if (questionArray[j] === answers[j]) {
      score = score + 1;
    }
  }

JS:

function submitAnswers() {

  //Set score and total number of questions
  var total = 5;
  var score = 0;
  //Get user input for each question
  var q1 = document.forms['quizForm']['q1'].value.toString();
  var q2 = document.forms['quizForm']['q2'].value.toString();
  var q3 = document.forms['quizForm']['q3'].value.toString();
  var q4 = document.forms['quizForm']['q4'].value.toString();
  var q5 = document.forms['quizForm']['q5'].value.toString();

  //Load Questions into Question Array
  var questionArray = [q1, q2, q3, q4, q5];

  //Validation
  for (var i = 0; i <= questionArray.length; i++) {
    if (questionArray[i] === null || questionArray[i] === '') {
      alert("Oops!  You forgot to answer a question.  Please enter an answer for Question " + [i + 1] + ".");
      return false;
    }
  }


  //Set correct Answers
  var answers = ["b", "a", "d", "b", "d"];

  //Check for correct answers
  for (var j = 0; i <= total.length; j++) {
    if (questionArray[j] === answers[j]) {
      score = score + 1;
    }
  }
  alert("You scoreed "+ score+ " out of "+total);



  return false;
}

HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple JavaScript Quiz</title>
    <link rel="stylesheet" href="css/main.css">

    <script src="js/script.js"></script>
  </head>
  <body>
    <div id="container">
      <header>
        <h1>Simple JavaScript Quiz</h1>
        <p>Test your knowledge in <strong>JavaScript fundamentals</strong></p>
      </header>
      <section>
        <div id="results"></div>
        <form name="quizForm" onsubmit="return submitAnswers()">
          <h3>1. In which HTML element do we put in JavaScript code?</h3>
          <input type="radio" name="q1" value="a" id="q1a">a. &lt;js&gt;<br>
          <input type="radio" name="q1" value="b" id="q1b">b. &lt;script&gt;<br>
          <input type="radio" name="q1" value="c" id="q1c">c. &lt;body&gt;<br>
          <input type="radio" name="q1" value="d" id="q1d">d. &lt;link&gt;<br>

          <h3>2. Which HTML attribute is used to reference an external JavaScript file?</h3>
          <input type="radio" name="q2" value="a" id="q2a">a. src<br>
          <input type="radio" name="q2" value="b" id="q2b">b. rel<br>
          <input type="radio" name="q2" value="c" id="q2c">c. type<br>
          <input type="radio" name="q2" value="d" id="q2d">d. href<br>

          <h3>3. How would you write "Hello" in an alert box?</h3>
          <input type="radio" name="q3" value="a" id="q3a">a. msg("Hello");<br>
          <input type="radio" name="q3" value="b" id="q3b">b. alertBox("Hello");<br>
          <input type="radio" name="q3" value="c" id="q3c">c. document.write("Hello");<br>
          <input type="radio" name="q3" value="d" id="q3d">d. alert("Hello");<br>

          <h3>4. JavaScript is directly related to the "Java" programming language</h3>
          <input type="radio" name="q4" value="a" id="q4a">a. True<br>
          <input type="radio" name="q4" value="b" id="q4b">b. False<br>

          <h3>5. A variable in JavaScript must start with which special character</h3>
          <input type="radio" name="q5" value="a" id="q5a">a. @<br>
          <input type="radio" name="q5" value="b" id="q5b">b. $<br>
          <input type="radio" name="q5" value="c" id="q5c">c. #<br>
          <input type="radio" name="q5" value="d" id="q5d">d. No Special Character<br>
          <br><br>
          <div id="submitBtn">
          <input class="btn" type="submit" value="Submit Answers">
          </div>
        </form>
      </section>
      <footer>
        <p>Copyright &copy; 2014, All Rights Reserved</p>
      </footer>
    </div>
  </body>
</html>

//Fixed Code Presentation

How to post Code

check my my updated answer at the bottom

3 Answers

I got it !

 for (var i + 0; i <= total; i ++) {
    if (questionArray[i] === answers[i]) {
      score = score + 1;

}

your variable total holds a number, numbers do not hold a length property i tested this and got a correct score

I Think it may be a little syntax error in the condition in your for loop: you have two variables I , and J present you wrote:

      for (var j = 0; i <= total.length; j++) {
    if (questionArray[j] === answers[j]) {
      score = score + 1;
    }
      }

try this:

    for (var i + 0; i <= total.length; i ++) {
    if (questionArray[i] === answers[i]) {
      score = score + 1;
    }
      }

Nope, that didn't fix it :(

//Fixed Code Presentation

How to post Code

Krondor says hi