4 Replies Latest reply on Mar 4, 2015 11:56 AM by Bernd Alheit

    What does "matchIndex!=-1" do?

    lindaeliseruble Level 1

      I would like to know what "matchIndex!=-1" does.

       

      I know that the bang means "not" so "!=" means not equal.

      I also understand that negative one "-1" means false.

      So I guess "!=-1" means not false or true?

      Put it all together and the above statement would mean that "matchIndex is true."

      Let's say that matchIndex is the index of a word found on a page.

      So what it's saying is that the index of a word on the page is true.  But compared to what?

      That is what I'm confused about.

       

      Here is an example I found on this forum with what I'm talking about in red:

      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

       

      var text = "Jack be nimble, Jack be quick, Jack jumped over the candlestick."

      var searchTerm = "Jack";

      var contextLength = 5;

       

      var matchesCounter = 0;

      var startIndex = 0;

      var matchIndex = text.indexOf(searchTerm,startIndex);

      while (matchIndex!=-1) {

          matchesCounter++;

          var context = text.substring(matchIndex-contextLength,matchIndex+searchTerm.length+contextLength);

          console.println("MATCH " + matchesCounter + " (" + matchIndex + "): " + context);

          startIndex = matchIndex+searchTerm.length;

          matchIndex = text.indexOf(searchTerm,startIndex);

      }

       

      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

       

       

      Thanks