Created
January 29, 2019 14:17
-
-
Save bluurn/65a7500349cce1d7782053f32a3a943a to your computer and use it in GitHub Desktop.
JS: Linear Search Implementation
This file contains 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
// return index of element if element is present in the array, overwise return -1 | |
var linearSearch = function (element, array) { | |
for(var i = 0; i < array.length; i++) { | |
if(element === array[i]) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
console.assert(linearSearch(6, [6,2,7,10]) == 0) | |
console.assert(linearSearch(11, [6,2,7,10]) == -1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment