string.indexOf() under the hood
Hello Developers, Today, I was trying to implement the indexOf() method from scratch. So far, this is what I've found: function myIndexOf(string, target, start = 0) { let l = target.length; // Adjust the start index if it's negative if (start < 0) { start = (string.length + start % string.length) % string.length; } console.log(start); // Loop through the string, looking for the target for (let i = start; i
Hello Developers,
Today, I was trying to implement the indexOf()
method from scratch. So far, this is what I've found:
function myIndexOf(string, target, start = 0) {
let l = target.length;
// Adjust the start index if it's negative
if (start < 0) {
start = (string.length + start % string.length) % string.length;
}
console.log(start);
// Loop through the string, looking for the target
for (let i = start; i <= string.length - l; i++) {
if (string.substr(i, l) === target) {
return i;
}
}
// Return -1 if the target isn't found
return -1;
}
console.log(myIndexOf("search me in me", "me", -34));
Code Explanation:
The indexOf() method accepts three parameters:
1.string: The string in which we are searching.
2.target: The substring we are looking for.
3.start: The index where the search will begin (defaults to 0).
First Attempt:
My initial idea was simple: loop through the string, and when I find string[i] === target, return i. If no match is found by the end of the loop, return -1. The code looked like this:
for (let i = 0; i < string.length; i++) {
if (string[i] === target) {
return i;
}
}
return -1;
However, this approach only works when the target is a single character, since we're comparing character by character.
Second Attempt:
I then realized that if the target is longer than one character, I need to compare substrings. I used the substr()
method to compare a substring of the same length as the target. The loop was adjusted to stop when there are enough characters left in the string to compare:
for (let i = 0; i < string.length - target.length + 1; i++) {
if (string.substr(i, target.length) === target) {
return i;
}
}
return -1;
Third Attempt:
Next, I needed to handle the start
parameter, which can be negative. The built-in indexOf()
method starts searching from string.length + start
when start is negative. For example, if the string length is 10 and the start is -4, the search will start at index 6 (i.e., 10 - 4).
To account for this, I updated the code to handle negative start values:
function myIndexOf(string, target, start = 0) {
let l = target.length;
if (start < 0) {
start = Math.max(0, string.length + start); // Adjust negative start
}
console.log(start);
for (let i = start; i <= string.length - l; i++) {
if (string.substr(i, l) === target) {
return i;
}
}
return -1;
}
Final Version:
Curious about handling start values greater than the string length, I decided to modify the function to keep "rolling around" the string if start exceeds the string length. This way, the function will continue searching from the appropriate index after wrapping around. The final solution uses this formula to adjust the start index:
start = (string.length + start % string.length) % string.length;
How It Works:
The modulo operation start % string.length ensures that start is within a range of -string.length to string.length.
Adding string.length ensures that any negative result becomes positive.
The final modulo operation ensures that the value of start is wrapped around and falls within valid index boundaries.
im thinking next to use binary search instead of the linear what do you think !