Skip to content

Instantly share code, notes, and snippets.

@NicholasEli
Created February 21, 2018 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicholasEli/3b0c2b363a2400cd8a8e7b37c9d3416d to your computer and use it in GitHub Desktop.
Save NicholasEli/3b0c2b363a2400cd8a8e7b37c9d3416d to your computer and use it in GitHub Desktop.
function stringPosition(needle, haystack) {
var m = needle.length,
n = haystack.length;
if (!m || !n || m > n) {
return -1;
}
if (m === n) {
return needle === haystack ? 0 : -1;
}
for (var j = 0; j < n; j++) {
for (var i = 0; i < m; i++) {
if (needle[i] !== haystack[i + j]) {
break;
}
if (i === m - 1) {
return j;
}
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment