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

CIW JavaScript Fundamentals - help please?

Discussion in 'CIW Certifications' 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. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Hi Guys ( and lasses)

    I've bumped this....
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  3. dmarsh

    dmarsh Terabyte Poster

    3,782
    302
    184
    Read this :-

    http://www.devguru.com/Technologies/ecmascript/QuickRef/string_lastindexof.html

    They appear to be 'trick' questions in that they are using the starting index to ensure that newbies fully understand the function.

    What does the book say ?

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

    ABRACADABRA".lastIndexOf("ABRA",10) == 7
    Starting from the end scanning backwards finds last (rightmost) ABRA, index 7.

    ABRACADABRA".lastIndexOf("ABRA",6); == 0
    Starting from 6 scanning backwards finds first (leftmost) ABRA, index 0.

    Why not just write a simple HTML page and test it ? If IE or Firefox differ from the book its probably a book typo, always rely on the evidence from multiple sources !
    Or you could try a JavaScript debugger ?
    Also remember indexes are zero based.
    Offsets appear to be from start string, algorithmn scans backwards.

    It confused me a little too, the fact that the algorithmn goes backwards and the offset is a starting point and not a terminating position.

     
    Certifications: CITP, BSc, HND, SCJP, SCJD, SCWCD, SCBCD, SCEA, N+, Sec+, Proj+, Server+, Linux+, MCTS, MCPD, MCSA, MCITP, CCDH
  4. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    Please don't cross post questions. I spent some time answering your first one, only to find you had posted it elsewhere as well.

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

    dee1810 Byte Poster

    142
    2
    29
    Harry

    My most sincere apologies to all.

    I usually only post to the CIW forum, and I didn't realise that there is a scripting forum.

    So sorry, Harry :oops:

    I was under extreme frustration, and not thinking at all.
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  6. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Hi DMarsh

    Yes, you have the correct answer. The first answer is 7 and then 0.

    I'll read both yours and Harrys answer later, and try to pound it into my head.
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl

Share This Page

Loading...