Created
December 13, 2017 20:48
-
-
Save aire-con-gas/3f372151885898cf5c7a98336d80c429 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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