[thelist] JavaScript Trauma

Teresa_Molina@hartehanks.com Teresa_Molina at hartehanks.com
Mon Mar 20 17:18:03 2000


" However,
I just can't seem to get my head round 'substring' and 'indexOf()'. Below is
a sample of an assignment from the book."

Basically, what your script seems to be doing (and I only glanced), at least in
relation to your questions, is simply returning a new integer in whole numbers.

Firstly, the "indexOf" returns an integer of where the character in parentheses
is within the context of the line. You need to know that JavaScript counts from
"0"; in a string such as "monkey," "m" is the character at 0, "k" is at 3, etc.
Without anything in those parantheses, indexOf is useless, as it doesn't know
what to look for to count.

(Conversly, there's a method in the String class of Java that does the opposite;
that is charAt(x) returns the character from an integer point. I'll bet there's
something similar in JS.)

Next, string.substring(x, y) returns a NEW string based on the beginning
integer, "0" or the beginning, and the last integer, whatever "." is in this
situation. In this case, it will return a new string that does not have any
decimal points in it.  So, if you've got "23.3092222", by going through those
hoops, your new string is "23."

Incidentally, I, as I'm sure many others, would strongly recommend the O'Reilly
JS book as a reference.

HTH.

As an aside, it seems that JavaScript isn't nearly as rigid about casting
primitive types. Yes?

Teresa