Created
April 6, 2019 06:01
-
-
Save ZackFox/e3097a03a91046aa21b66bf7af99d53a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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