Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement