Skip to content Skip to sidebar Skip to footer

Why Is Indexof(char) Is 0 If The Value Exists?

Why is firstHalf.indexOf(secondHalfArr[i]) 0?

Solution 1:

The indexOf() method returns the position of the first occurrence of a specified value in a string.

This method returns -1 if the value to search for never occurs. The indexOf() method is case sensitive.


Make sure to use !== -1 in your statements for valid results.

Example:

for (let i = 0; i < secondHalfLength; i++) {
  if (secondHalf.indexOf(firstHalfArr[i]) !== -1) {
    charsToReplace++;
  }
}

Post a Comment for "Why Is Indexof(char) Is 0 If The Value Exists?"