Advertisement
Guest User

Untitled

a guest
May 27th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. var strStr = function( needle,haystack ) {
  2.  
  3. // needle cannot be longer than haystack
  4. if(haystack.length < needle.length)
  5. return null;
  6. // needle is empty string
  7. if( needle.length === 0){
  8. return null;
  9. }
  10. // both strings are same
  11. if(haystack == needle){
  12. return true;
  13. }
  14.  
  15. // lookahead to check needle in haystack
  16. for(var i=0; i < haystack.length;i++){
  17. var matching = false;
  18. if(haystack[i] === needle[0]){
  19. matching = true;
  20. var count = 1;
  21. while(matching){
  22. // all characters in the needle have been checked
  23. if(count==needle.length) {
  24. return true;
  25. }
  26. if(haystack[i+count] !== needle[count]){
  27. matching=false;
  28. }
  29. count++;
  30. }
  31. }
  32. }
  33. return null;
  34. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement