Last active
August 29, 2015 14:10
-
-
Save stoimen/01edc5c3fdaca4f1764e to your computer and use it in GitHub Desktop.
Sequential Search (js)
This file contains hidden or 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() { | |
var sequential = function(haystack, needle) { | |
var i = 0, | |
len = haystack.length | |
; | |
for (; i < len; i++) { | |
if (haystack[i] === needle) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
})(); |
This file contains hidden or 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() { | |
var sequential_v1 = function(haystack, needle) { | |
return haystack.filter(function(element) { | |
return element === needle | |
}).length > 0; | |
}; | |
})(); |
This file contains hidden or 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() { | |
var search = function(haystack, needle) { | |
var l = haystack.length; | |
while (haystack[--l] !== needle); | |
return l > -1; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment