Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6656399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:38:17+00:00 2026-05-26T01:38:17+00:00

I’m making a simple client-side, self-grading quiz. I ask 6 questions and I want

  • 0

I’m making a simple client-side, self-grading quiz.

I ask 6 questions and I want to alert the user with their score (keeping things simple). If they leave an answer blank, an alert will appear.

I’m new to javascript and don’t really know how to check individual form elements to see if they’re empty. I’m also having problems with getting my code to run.

JS

EDIT

function grade() {
    var score = 0;
    var elt = document.quiz;

    // Check to see if no questions were left unanswered.
    if elt.question1.value.length == 0 || elt.question2.value.length == 0 ||   
       elt.question3.value.length == 0 || elt.question4.value.length == 0 ||
       elt.question5.value.length == 0 || elt.question6.value.length == 0
      {
        alert ("Whoops, you're missing an answer!")
    }

    if (elt.question1[1].checked) {
        score += 1;
    }
    if (elt.question2[0].checked) {
        score += 1;
    }
    if (elt.question3[0].checked == false && elt.question3[1].checked &&  
        elt.question3[2].checked == false && elt.question3[3].checked == false) {
        score += 1;
    }
    if (elt.question4[0].checked == false && elt.question4[1].checked == false &&
        elt.question4[2].checked == false && elt.question4[3].checked) {
        score += 1;
    }
    elt.question5 = elt.question5.toLowerCase()
    if (elt.question5.value != '' && elt.question5.value.indexOf('galaxy') != -1) {
        score += 1;
    }
    elt.question6 = elt.question6.toLowerCase()
    if (elt.question5.value != '' && elt.question6.value.indexOf('age') != -1) {
        score += 1;
    }

    score = score / 6 * 100;
    score = score.toFixed(2);
    alert("You scored " + score + "%");

    return false; // Return true if you want the form to submit on validation/grade
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:38:18+00:00Added an answer on May 26, 2026 at 1:38 am

    You have a some significant errors in your markup:

    1. Do not wrap a form element around each question. These should all be in one form element. (Also, each question be in a OL to number the question in series.)
    2. You’re not properly closing all of your label‘s, so they’re selecting other elements when you click them (try question 3, first checkbox).
    3. You need the grade() function on the form’s submit handler, and it needs to be onsubmit="return grade()", with grade() returning false when it doesn’t “pass” to prevent form submission*.

    * Note, I set the grade() function to always return false in the example. You would need to add the logic for when it would allow the form to submit.

    As far as the Javascript…

    You need the elt variable to be equal to your document.quiz (note, I changed the main form to have a name="quiz" in your markup). You can use indexOf() instead of a regex if you just want to have a simple check (regex could check for age as a word, though).

    If you just want to make sure a text input is not empty, you can use el.value.length != 0 or el.value != ''.

    Also, looking at your grading code, if you want only one to be selected, you could use a radio, unless you want the person taking the quiz to not know if one or more were valid answers. But radio’s only allow you to select a single value.

    HTML

    <h3> Self-Grading Astronomy Quiz </h3>
    <form action="" name="quiz" onsubmit="return grade();">
     <p>1. According to Kepler the orbit of the earth is a circle with the sun at the center.</p>
     <p>
      <label><input type="radio" name="question1" value="true" /> True </label>
      <label><input type="radio" name="question1" value="false" /> False </label>
     </p>
     <p>2. Ancient astronomers did consider the heliocentric model of the solar system but rejected it because they could not detect parallax.</p>
     <p>
      <label><input type="radio" name="question2" value="true" /> True </label>
      <label><input type="radio" name="question2" value="false" /> False </label>
     </p>
     <p>3. The total amount of energy that a star emits is directly related to its:</p>
     <p>
      <label><input type="checkbox" name="question3" value="1" /> a) surface gravity and magnetic field </label><br/>
      <label><input type="checkbox" name="question3" value="2" /> b) radius and temperature </label><br/>
      <label><input type="checkbox" name="question3" value="3" /> c) pressure and volume </label><br/>
      <label><input type="checkbox" name="question3" value="4" /> d) location and velocity </label>
     </p>
     <p>4. Stars that live the longest have:</p>
     <p>
      <label><input type="checkbox" name="question4" value="1" /> a) high mass </label><br/>
      <label><input type="checkbox" name="question4" value="2" /> b) high temperature </label><br/>
      <label><input type="checkbox" name="question4" value="3" /> c) lots of hydrogen </label><br/>
      <label><input type="checkbox" name="question4" value="4" /> d) small mass </label>
     </p>
     <p>5. A collection of a hundred billion stars, gas, and dust is called a __________.</p>
     <p>
      <input type='text' id='question5' />
     </p>
     <p>6. The inverse of the Hubble's constant is a measure of the __________ of the universe.</p>
     <p>
      <input type='text' id='question6' />
     </p>
     <p>
      <input type='button' onclick='grade()' value='Grade' />
     </p>
    </form>
    

    Javascript

    function grade() {
        //**Would I do something like this?
        //if(elem.value.length == 0){
        // alert("Whoops, looks like you didn't answer a question.")}
        var score = 0;
        var elt = document.quiz;
    
        if (elt.question1[1].checked) {
            score += 1;
        }
        if (elt.question2[0].checked) {
            score += 1;
        }
        if (elt.question3[0].checked == false && elt.question3[1].checked && elt.question3[2].checked == false && elt.question3[3].checked == false) {
            score += 1;
        }
        if (elt.question4[0].checked == false && elt.question4[1].checked == false && elt.question4[2].checked == false && elt.question4[3].checked) {
            score += 1;
        }
        if (elt.question5.value != '' && elt.question5.value.indexOf('galaxy') != -1) {
            score += 1;
        }
        if (elt.question5.value != '' && elt.question6.value.indexOf('age') != -1) {
            score += 1;
        }
    
        score = score / 6 * 100;
        score = score.toFixed(2);
        alert("You scored " + score + "%");
    
        return false; // Return true if you want the form to submit on validation/grade
    }
    

    http://jsfiddle.net/BeD3Z/10/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.