Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. function find(needle, heystack) {
  2. let returnValue = false;
  3. let indexNeedle = 0;
  4. heystack.split("").forEach(element => {
  5. if (needle[indexNeedle] === element) {
  6. indexNeedle += 1;
  7. if (indexNeedle === needle.length) {
  8. return false; // break free the cycle
  9. }
  10. }
  11. });
  12. if (indexNeedle === needle.length) {
  13. returnValue = true;
  14. }
  15. return returnValue;
  16. }
  17.  
  18. console.log(
  19. find("ale", "appleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")
  20. ); // true
  21. console.log(find("ael", "apple")); // false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement