Skip to content

Instantly share code, notes, and snippets.

@bluurn
Created January 29, 2019 14:17
Show Gist options
  • Save bluurn/65a7500349cce1d7782053f32a3a943a to your computer and use it in GitHub Desktop.
Save bluurn/65a7500349cce1d7782053f32a3a943a to your computer and use it in GitHub Desktop.
JS: Linear Search Implementation
// 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