ChaiTea
ChaiTea

Reputation: 65

JavaScript: looping through string with 'for' behaving unexpectedly

This is a two part question.

I wrote a function to find the index of a substring inside a bigger string with the following function

function indexOf(str){
  for (var x = 0; x < this.length; x++) {
    if(this[x] === str) {
      return x;
    }
  }
    return -1;
  };

I then set:

var string1="bbbabbaaa"
var string2="ab"

Then I called my function in this manner:

string1.indexOf(string2)

I was expecting to see -1 in the result because I was expecting the for loop to go through the characters in the string one by one and compare each letter to the string "ab". Since no single letter is equivalent to "ab", it shouldn't find a match. But what actually returned was 3, which is the index of "a". So my first question is, what happened here? how did the if(this[3] === str) return true, isn't it basically comparing "a" with "ab", which should return false right?

if I modify my function to pass string1 in as an argument, I get the expected -1:

function AlternativeIndexOf(str,str2){
  for (var x = 0; x < str.length; x++) {
    if(str[x] === str2) {
      return x;
    }
  }
    return -1;
  };

calling the function this way AlternativeIndexOf(string1, string2), returns -1.

So my second question is, hows is calling the function AlternativeIndexOf(string1, string2) differ from calling the function string1.indexOf(string2)

Upvotes: 0

Views: 45

Answers (2)

autistic
autistic

Reputation: 15632

For a start, as another answer has mentioned, you need to assign to String.prototype.indexOf.

So my first question is, what happened here? how did the if(this[3] === str) return true, isn't it basically comparing "a" with "ab", which should return false right?

Your function isn't being called. The function being called is String.prototype.indexOf (which you haven't overwritten), and it's behaving correctly; your understanding of String.prototype.indexOf is broken. See the String.prototype.indexOf manual for more information. Here's an example, from that manual, of what String.prototype.indexOf should return when the second argument is a string:

'Blue Whale'.indexOf('Blue'); // returns 0

You have two strings; you should be using one loop nested inside of another to loop on characters of both strings, comparing them. For example:

String.prototype.indexOf = function(str) {
    for (var x = 0; x < this.length; x++) {
        var y = 0;
        while (y < str.length && this[x+y] == str[y]) {
            y++;
        }
        if (y == str.length) {
            return x;
        }
    }
    return -1;
};

Upvotes: 0

void
void

Reputation: 36703

string1.indexOf(string2)

This is using Javascript native String.prototype.indexOf() method and not the one created by you, you are trying to override the .indexOf function but the way you are doing it is not correct, instead try to redefine the String.prototype.indexOf

String.prototype.indexOf = function(elem) {
    // Put your logic here
}

Upvotes: 2

Related Questions