Skip to content

Instantly share code, notes, and snippets.

@helabenkhalfallah
Created July 10, 2022 17:25
Show Gist options
  • Save helabenkhalfallah/7bbb5bf51bcb79ee60b0708b4ed5ae7f to your computer and use it in GitHub Desktop.
Save helabenkhalfallah/7bbb5bf51bcb79ee60b0708b4ed5ae7f to your computer and use it in GitHub Desktop.
Vintage Linear Search
const vintageLinearSearch = (data, target) => {
for (let i in data) {
if (data[i] === target) return i
}
return -1
}
console.log(vintageLinearSearch([1, 2, 3, 4], 1)) // 0
console.log(vintageLinearSearch([1, 2, 3, 4], 4)) // 3
console.log(vintageLinearSearch([1, 2, 3, 4], 6)) // -1
console.log(vintageLinearSearch([3, 4, 1, 6, 3], 6)) // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment