Skip to content

Instantly share code, notes, and snippets.

@serabakpak
Last active February 15, 2017 06:43
Show Gist options
  • Save serabakpak/4ea9c003404d22d73f6af14d58dfce2f to your computer and use it in GitHub Desktop.
Save serabakpak/4ea9c003404d22d73f6af14d58dfce2f to your computer and use it in GitHub Desktop.

#Write our own indexOf utility method

String.prototype.indexOf = function(pattern) {
  for (var i = 0; i < this.length; i++) {
    var potentialMatch = this.substring(i, pattern.length + i);
    if (potentialMatch === pattern) {
      return i;
    }
  }
  
  return -1;
}

console.log("happy happy days at everlane".indexOf("appy days")); //--> 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment