Skip to content

Instantly share code, notes, and snippets.

@aire-con-gas
Created December 13, 2017 20:48
Show Gist options
  • Select an option

  • Save aire-con-gas/3f372151885898cf5c7a98336d80c429 to your computer and use it in GitHub Desktop.

Select an option

Save aire-con-gas/3f372151885898cf5c7a98336d80c429 to your computer and use it in GitHub Desktop.
const findSubstr = (needle, haystack) => {
let atIdx = -1;
let found = false;
const needleArr = needle.split('');
const haystackArr = haystack.split('');
for(let i = 0; i <= haystackArr.length; i++) {
found = true;
if (needle[0] === haystackArr[i]) {
for(let j = 0; j <= needleArr.length; j++) {
if (needle[j] !== haystackArr[i+j]) {
found = false;
break;
}
}
atIdx = i;
break;
}
}
return (found ? atIdx : -1);
}
console.log('find sseiro in travesseiro', findSubstr('sseiro', 'travesseiro'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment