Note that there are some explanatory texts on larger screens.

plurals
  1. POError when calling JavaScript function — "can't find variable"
    primarykey
    data
    text
    <p>I'm attempting to complete and exercise from the JavaScript Bible, and am having trouble getting my script to function.</p> <p>The assignment is to create a page that allows users to query a planet's name, and, via a script that matches the planet's name with its data stored in the associate arrays, call up its distance and diameter information.</p> <p>I'm attempting to call the function 'getPlanetInfo' via a button (onclick='getPlanetInfo()'). However, my error console reports that it cannot find a variable named 'getPlanetInfo' when I attempt to run it.</p> <p>I've attached both my JS and HTML code below. Any idea as to why my function isn't being properly called would be hugely appreciated.</p> <p>HTML:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; ... &lt;script type="text/javascript" src="planets.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Check a planet's distance from the sun and its diameter!&lt;/h1&gt; &lt;form&gt; &lt;input type="text" name="entry" id="entry"&gt; &lt;input type="button" value="Check it!" onClick="getPlanetInfo()"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>JS:</p> <pre><code>var planetNames = new Array(4); planetNames[0] = "Mercury"; planetNames[1] = "Venus"; planetNames[2] = "Earth"; planetNames[3] = "Mars"; var planetDistances = new Array(4); planetDistances[0] = "36 million miles"; planetDistances[1] = "67 million miles"; planetDistances[2] = "93 million miles"; planetDistances[3] = "141 million miles"; var planetDiameters = new Array(4); planetDiameters[0] = "3,100 miles"; planetDiameters[1] = "7,700 miles"; planetDiameters[2] = "7,920 miles"; planetDiameters[3] = "4,200 miles"; function getPlanetInfo() { var selectedPlanet = document.getElementById("entry").value; for (var i = 0; i &lt; planetNames.length; i++) { if (planetNames[i] == selectedPlanet) { break; } } if (i &lt; planetNames.length) { alert(selectedPlanet + " is " + planetDistances[i] " in distance from the Sun and " + planetDiameters[i] + "in diameter.") } else { alert("Sorry, " + selectedPlanet + " isn't in the database."); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload