1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Please help a JavaScript newbie

Discussion in 'Scripting & Programming' started by dee1810, Jun 24, 2007.

  1. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Hi all
    I'm really stumped here..
    Here is the question...quick background.

    The lastIndexOf method, lastIndexOf(searchText, endingIndexPosition) returns the index value for the starting position of the last occurence of the search text specified.
    The method counts from the ending position instead of the starting position of the string.
    "ABRACADABRA".lastIndexOf("ABRA",10);
    "ABRACADABRA".lastIndexOf("ABRA",6);

    Now the course book gives me results for this -but, I can't get it to the same answer. This has completely confused me.

    Please could someone explain this method too me, in English?
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  2. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    I'd say that the first one should give the answer 7, and the second one the answer 0.

    First a warning - I'm no expert on JS. :biggrin

    The search is done *backwards* , but the result is counted from the *start* of the array. In the first line the second param of 10 is actualy the length of the array, so it finds the last "ABRA" in the word. In the second line the search is started at offset 6 from the start, so will only find the first "ABRA".

    Now to construct a page with it in to see if I'm right....

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  3. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Harry

    You are absolutely right. You have the correct answer.
    What gets me is the fact that the first statement gives the number 10, so I count backwards from 0 and I get to the end of the String, and find that I am on the first letter A and not the end of the forst instance of ABRA.
    See, I am confused...
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  4. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    The idea of the function is to find the 'last' instance of the search key, where 'last' here means reading from left to right, as usual in English.

    So clearly, in the simple case, you start from the right and compare the key to the original, moving *left* each time there is no match, until you get a match or run out of original.

    The second parameter is there to allow you to vary the rules a bit. Setting a position there shows where you should start from, rather than starting at the right-hand edge as in the simple case.

    When the second param is positive, as it is here in both examples, you count from the *left* that many places. The place you wind up at is where you start the search from.

    All offsets, both the second parameter and the result are *forwards*. The only backwards bit is how you move each time the match fails.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  5. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    10 9 8 7 6 5 4 3 2 1 0
    A B R A C A D A B R A

    So, count forward 10 places leaves you at the end of the string. In other words, the 10 may as well not have been there at all because you are taking the FULL 11 letters into consideration.
    So all you do is find the LAST instance of the string ABRA, then go back to the very start of the WHOLE string and start counting (from 0) until you reach the first letter in the last instance of the desired string (A, in ABRA, in our case). The A is at position 7 from the left.


    10 9 8 7 6 5 4 3 2 1 0
    A B R A C A D A B R A

    For the second method, you count forward 6 places form the start of the whole string (starting with 0). You then effectively discard all the letters after that (i.e positions 3 2 1 and 0). Now since the ONLY instance of the string ABRA (in the remaining Part of the string) starts from the very first position, then the answer is 0.

    The course manual is WRONG. At NO TIME do you COUNT backwards (except, as Harry said, when the last argument is negative). Its best to totaly ignore that sentence Dee.
    This is the reason I have had so many problems..Incorrect/missleading/confusing information in the course manuals.:x
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)
  6. dmarsh

    dmarsh Terabyte Poster

    3,782
    302
    184
    You never COUNT backwards, you SCAN backwards !

    The string is basically a character array internally, it has 11 characters, therefore the array is of length 11, being zero based the indexes into the array range from 0-10.

    The algorithm scans for a matching substring from the rightmost end or highest index of the string, it scans backwards or leftwards towards the lowest index.

    The second parameter or offset effectively limits the scan to characters to the left of that position, the characters to the right of the postition are effectively ignored or discarded for the purposes of the scan.
     
    Certifications: CITP, BSc, HND, SCJP, SCJD, SCWCD, SCBCD, SCEA, N+, Sec+, Proj+, Server+, Linux+, MCTS, MCPD, MCSA, MCITP, CCDH
  7. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    Not having the manual I wasn't aware that it contained an error!

    I checked the definition of the function with the "Core JavaScript Reference" on the Mozilla site.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+

Share This Page

Loading...