Hi forum users.
I am trying to write a quiz in JavaScript and I need to have the JS checking the quiz in a separate file than the quiz web page. When I run the following codes, I get the error order is not a function.

Can someone tell me what the error means?

<html>
<head>
<title> Quiz</title>
<script type = "text/javascript" src = "order.js"></script>
</head>
<body>
<form id = "order" action = "" method = "post" >
<label>A collection of a hundred billion stars, gas, and dust is called a</label><br />
<input type="text" name="collection"
size="10" maxlength="25"><br />
<label>The inverse of the Hubble's constant is a measure of the ___ of the universe.</label><br />
<input type="text" name="Hubble"
size="10" maxlength="25"><br />

<label> Stars that live the longest have </label><br />
<input type="checkbox" name="stars" value="small mass" />small mass<br />

<input type="checkbox" name="stars" value="high mass" />high mass <br />

<input type="checkbox" name="stars" value="high temperature" />high temperature<br />

<input type="checkbox" name="stars" value="lots of hydrogen" />lots of hydrogen<br />
<label>The total amount of energy that a star emits is directly related to its</label><br />
<input type="checkbox" name="energy" value="surface gravity and magnetic field" />surface gravity and magnetic field<br />

<input type="checkbox" name="energy" value="radius and temperature" />radius and temperature<br />

<input type="checkbox" name="energy" value="pressure and volume" />pressure and volume<br />

<input type="checkbox" name="energy" value="location and velocity" />location and velocity<br />
<label>According to Kepler the orbit of the earth is a circle with the sun at the center.</label><br /> 
<input type="radio" name="question3" value="True"  />True
<input type="radio" name="question3" value="False"  />False<br />
<label>Ancient astronomers did consider the heliocentric model of the solar system </label><br />
but rejected it because they could not detect parallax. </label>
<input type="radio" name="question5" value="True"  />True
<input type="radio" name="question5" value="False"  />False<br />



<input type = "button" name= "check" value = "calculate" onclick="order();" />
<input type = "reset" value = "Clear Form" /><br />
<br /><br />


</form>

</body>
</html>
function order()
{ 
  alert("hello");
  var elt = document.getElementById("order");
  
  var score = 0;
  var question1= elt.collection.value;
  var question2= elt.Hubble.value;
  var test= False;
  var ans;
  
  if (question1=="galaxy")
  {
    score= score + 1;
  }
  if (question2=="age")
  {
    score= score + 1;
  }
  for (var index =0; index < elt.question5.length; index++)
  {
     if (elt.question5[index].checked)
     { 
       ans= elt.question[index].value;
       
     }
  }
      
  if (elt.question3[1].checked)
  {
    score= score+ 1;
  }
  if (elt.question5[0].checked)
  {
    score= score + 1;
  }
  if (elt.energy[1].checked)
  {
    score= score +1;
  }
  if (elt.stars[0].checked)
  {
    score= score + 1;
  }
  window.alert(score);
  

}

Recommended Answers

All 2 Replies

Hmm.. I have no problem calling "order()" function in your script? Are you sure that your .js file is in the same folder as your .html file? Also, what browser are you using?

There are a couple script errors I see so far is

var test= False;

The "False" is not a boolean value, it must be "false" instead. Another one is

ans= elt.question[index].value;

which said elt.question is undefined because it should be ans=elt.question5[index].value;

I second Taywin's motion.

At the bottom of "foo.js" I always place this line:

alert( 'foo.js loaded' );

If the alert pops up, you have no avoided several of the ways to screw up a .js file: wrong directory, wrong name, syntax error in file, ... Then you comment out the alert and proceed.

If the alert doesn't pop up, you start checking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.