Created
August 8, 2019 15:25
-
-
Save eduarmreyes/8e512a7735ab27eb79489264493ac388 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 find(needle, heystack) { | |
let returnValue = false; | |
let indexNeedle = 0; | |
heystack.split("").forEach(element => { | |
if (needle[indexNeedle] === element) { | |
indexNeedle += 1; | |
if (indexNeedle === needle.length) { | |
return false; // break free the cycle | |
} | |
} | |
}); | |
if (indexNeedle === needle.length) { | |
returnValue = true; | |
} | |
return returnValue; | |
} | |
console.log( | |
find("ale", "appleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") | |
); // true | |
console.log(find("ael", "apple")); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment