Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var strStr = function( needle,haystack ) {
- // needle cannot be longer than haystack
- if(haystack.length < needle.length)
- return null;
- // needle is empty string
- if( needle.length === 0){
- return null;
- }
- // both strings are same
- if(haystack == needle){
- return true;
- }
- // lookahead to check needle in haystack
- for(var i=0; i < haystack.length;i++){
- var matching = false;
- if(haystack[i] === needle[0]){
- matching = true;
- var count = 1;
- while(matching){
- // all characters in the needle have been checked
- if(count==needle.length) {
- return true;
- }
- if(haystack[i+count] !== needle[count]){
- matching=false;
- }
- count++;
- }
- }
- }
- return null;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement