6

I'm attempting to complete and exercise from the JavaScript Bible, and am having trouble getting my script to function.

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.

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.

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.

HTML:

<!DOCTYPE html>
<html>
<head>
    ...
    <script type="text/javascript" src="planets.js"></script>
</head>
<body>
        <h1>Check a planet's distance from the sun and its diameter!</h1>
        <form>
            <input type="text" name="entry" id="entry">
            <input type="button" value="Check it!" onClick="getPlanetInfo()">
        </form>
</body>
</html>

JS:

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 < planetNames.length; i++)
{
    if (planetNames[i] == selectedPlanet)
    {
        break;
    }
}

if (i < 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.");
}
}
5
  • Please note I haven't bothered to indent my code properly above; rest assured it is properly indented in the actual files. ;) Commented Jan 21, 2012 at 23:37
  • 1
    The moment it takes to paste&copy your code to jsbeautifier makes people much more likely to help you. Commented Jan 21, 2012 at 23:39
  • 2
    @FourSix - If you can't be bothered to make it readable, then why should we be bothered to read it? Commented Jan 21, 2012 at 23:41
  • 1
    Thanks to all for the advice. @lwburk — I appreciate the sentiment, but I hardly think the code is unreadable. Commented Jan 21, 2012 at 23:53
  • code as a communication tool slideshare.net/macskeptic/code-as-a-communication-tool Commented Jan 22, 2012 at 10:08

2 Answers 2

2

This line:

alert(selectedPlanet + " is " + planetDistances[i] " in distance from the Sun and " + planetDiameters[i] + "in diameter.")

is missing a + sign after planetDistances[i], so the function has a syntax error and is not created, and naturally it's not found when called.

http://www.jsfiddle.net helps you create a reproducible case that we can all see, use it when you need to ask js questions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for catching this and for the tip, Marcelo.
0

You're missing a + - this:

alert(selectedPlanet + " is " + planetDistances[i] " in distance from the Sun and " + planetDiameters[i] + "in diameter.")

should be

alert(selectedPlanet + " is " + planetDistances[i] + " in distance from the Sun and " + planetDiameters[i] + "in diameter.")

You should use something like Firebug to catch syntax errors when loading your script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.