Re: Newbiee JS problem

by "Greg Rundlett" <greg(at)freephile.com>

 Date:  Sun, 23 Jul 2000 07:37:47 -0400
 To:  "David Andrew" <davida51(at)swbell.net>,
<hwg-languages(at)hwg.org>
 References:  swbell
  todo: View Thread, Original
Here's some code that I worked out, unfortunately I don't have time to
finish debugging it.  But I hope this helps.
Note, I created a function that will allow you to pass any array object as
the argument, and it will list the contents of the array.
There's probably a built-in function to do this--I just don't remember it.
Also, you should put semicolons after each line of code, even if the
interpreter will place them automatically.
One thing that is not causing problems, but is misleading is that I believe
that new Array(1); creates an array with one element, that is element[0].
When you create the second element, JavaScript automatically expands the
size of your array to accomodate the new family member.  If you just want
JavaScript to automatically size your array, use new Array(); with no size
argument.
<html>
<head>


<SCRIPT LANGUAGE=JavaScript>

var USStates = new Array(1);
USStates[0] = "Alabama";
USStates[1] = "Alaska";
// alert (USStates);

var stateEntered = new Array(1);
stateEntered[0] = 1819;
stateEntered[1] = 1959;

function listArrayElements (arrayObject) {
  var listingArrayElements = "";
  for (var j=0; j<arrayObject.length; j++) {
    listingArrayElements += (arrayObject[j] + "\n");
    }
  return (listingArrayElements);
  }
alert (listArrayElements(stateEntered));

function getStateDate()
{
 var selectedState = document.entryForm.entry.value;
 alert (selectedState);
  for ( var i = 0; i < USStates.length; i++)
  {
   if (USStates[i] == selectedState)
    {
      alert(" that State entered the Union in " + stateEntered[i] + ".");
            break;
    }
  }


}
</SCRIPT>

</head>


<body>
<form name="entryForm">
Enter the name of a state:
<BR>
<input type ="text" name="entry">
<br>
<input type="button" value="Look up the date the State entered the Union."
"onClick="getStateDate();">
</form>



</body>
</html>

----- Original Message -----
From: David Andrew <davida51(at)swbell.net>
To: <hwg-languages(at)hwg.org>
Sent: Saturday, July 22, 2000 10:39 PM
Subject: Newbiee JS problem


> Hi All -
>
> I am going through the "JavaScript Bible 3rd Edition" and have problems
> with problem 7-1.
>
> The script can be found at
> http://www.gggraphics.net/JS_Class/cp_7_1.htm
>
> When I validate in NS,  I get for line 18:
>
> syntax error.
>
>         for ( var i = 0; < USStates.length; i++)
> ...............................^
>
> I'm hoping fresh eyes will be able to tell me my mistake.
>
> TIA
>
> David Andrew
>

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA