Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2010
    Location
    London
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Slightly Confused by code

    Hi,

    I've been learning javascript from a fairly decent book and things are going quite well, however, theres an element to the latest task that I'm not understanding very well and was hoping someone on here could help with this:
    Code:
    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
    <html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
    <title>Chapter 5: Example 1</title>
    </head>
    <body>
    <script type=”text/javascript”>
    var myString = “Welcome to Wrox books. “;
    myString = myString + “The Wrox website is www.wrox.com. “;
    myString = myString + “Visit the Wrox website today. Thanks for buying Wrox”;
    var foundAtPosition = 0;
    var wroxCount = 0;
    while (foundAtPosition != -1)
    {
    foundAtPosition = myString.indexOf(“Wrox”,foundAtPosition);
    if (foundAtPosition != -1)
    {
    wroxCount++;
    foundAtPosition++;
    }
    }
    document.write(“There are “ + wroxCount + “ occurrences of the word Wrox”);
    </script>
    </body>
    </html>
    The part I'm not understanding is the foundAtPosition part, the != -1 part seems silly to me it will never be equal to this and therefore the while loop would continually loop over and over.
    I know it's not because I've run this code and it performs correctly I just wondered why the loop doesnt continually loop over and why would a value hit -1 at some point.

    Many thanks

    Sara

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts
    Hi sara, the foundAtPosition is going to be equal to the first occurrence of the word "Wrox" that it finds.

    When you call indexOf(), the first parameter is the string you're searching for, and the second parameter is where to start in the string. indexOf() will either return the element number where it found the string you're searching for, or it will return -1 if it can't find it.

    So let's say we have This string:
    "My Wrox is Wrox."

    The first time through, it will find Wrox at myString[3], then if you move the start point, it will find it at myString[11], then if you repeat that process again, the result will be -1 because it will not be found.

  • Users who have thanked Dean440 for this post:

    sara jane (06-19-2010)

  • #3
    New Coder
    Join Date
    Jun 2010
    Location
    London
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi Dean440,

    Perfect!! I understand now.

    Thanks alot


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •