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

Salvador Cervantes
Salvador Cervantes
10,898 Points

Help with JS Error

I'm having an issue with small project that im doing for fun. but I have gone blank for some reason. can one look at my code and see what in the world im doing wrong.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Do you know games!</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
    <link rel='stylesheet' type='text/css'  href='./css/main.css'>
  </head>
  <body>
    <div>
      <!--  Intro -->
      <div class='header container'>
        <div class='row'>
          <div class='col-4'></div>
          <div class='col-4'>
            <div class='header-p'>
              <p>
                Do you know
              </p>
            </div>
          </div>
          <div class='col-4'></div>
        </div>
        <div class='row'>
          <div class='col-4'></div>
          <div class='col-4'>
            <div class='header-subject'>
              <p>
                test?
              </p>
            </div>
          </div>
          <div class='col-4'></div>
        </div>
      </div>

      <!-- Question -->
      <div class='question container'>
        <div class='row'>
          <div class='col-2'></div>
          <div id='textArea' class='col-8 quiz-text'>
            <h3>
              <p>
                The following are some questions about <span>test</span>, lets see how many you get right out of 3!
              </p>
            </h3>
          </div>
          <div class='col--2'></div>
        </div>

        <div class='row'>
          <div class='col-2'></div>
          <div class='col-8'>
            <form id='quiz'>
              <div>
                <p>
                  What year was test open?
                </p>
              </div>
              <div>
                <input type=radio id='option1' name='question1' value='1999'>1999</input>
                <input type=radio id='option1' name='question1' value='2000'>2000</input>
              </div>
              <div>
                <input type=radio id='option1' name='question1' value='1989'>1989</input>
                <input type=radio id='option1' name='question1' value='2010'>2010</input>
              </div>

              <div>
                <p>
                  How many stores are there?
                </p>
              </div>
              <div>
                <input type=radio id='option2' name='question2' value='100'>100</input>
                <input type=radio id='option2' name='question2' value='200'>200</input>
              </div>
              <div>
                <input type=radio id='option2' name='question2' value='250'>250</input>
                <input type=radio id='option2' name='question2' value='300'>300</input>
              </div>

              <div>
                <p>
                  What year was test open?
                </p>
              </div>
              <div>
                <input type=radio id='option3' name='question3' value='John'>John</input>
                <input type=radio id='option3' name='question3' value='smith'>smith</input>
              </div>
              <div>
                <input type=radio id='option3' name='question3' value='berry'>berry</input>
                <input type=radio id='option3' name='question3' value='tommy'>tommy</input>
              </div>

              <div>
                <button  class='quiz-button' type=button id='finish' onClick='quiz()'>Finish</button>
              </div>
            </form>
            </div>
        </div>
        <div class='col-2'></div>
      </div>

    </div>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
    <script src='./js/main.js'></script>
  </body>
</html>

JS

function quiz() {

  const q1 = document.quiz.question1.value;
  const q2 = document.quiz.question2.value;
  const q3 = document.quiz.question3.value;
  var count = 0;

  if(q1 === '1999') {
    count++;
  }

  if(q2 === '300') {
    count++;
  }

  if(q3 === 'John'){
    count++;
  }

  var message = ['you suck!', 'you did okay.', 'you did amazing'];

  if(count >= 0) {
    alert(message[0]);
}
  if (count >= 1 && count < 3){
    alert(message[1]);
  }

  if (count === 3) {
    alert(message[2]);
  }
}

1 Answer

Steven Parker
Steven Parker
229,644 Points

I'm not sure what's going on here (and the other 2 lines like it):

  const q1 = document.quiz.question1.value;

The closest thing I can think of that would do what I think you want would be:

  const q1 = (document.querySelector('input[name="question1"]:checked')||{value: null}).value;