Skip to content

Instantly share code, notes, and snippets.

@ZackFox
Created April 6, 2019 06:01
Show Gist options
  • Save ZackFox/e3097a03a91046aa21b66bf7af99d53a to your computer and use it in GitHub Desktop.
Save ZackFox/e3097a03a91046aa21b66bf7af99d53a to your computer and use it in GitHub Desktop.
function indexOf (string, sub) {
if(string === sub || !sub) return 0;
if(!string ) return -1;
let res = -1;
let k = 0;
for(let i = 0 ; i < string.length; i++){
if(string.charAt(i) === sub.charAt(k)){
k++;
if(k === sub.length){
res = i + 1 - sub.length;
break;
}
}else{
i = i - k;
k = 0;
}
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment