for...in
loop to iterate over the indexes of the given array.item
.+
operator to convert it from a string to a number.-1
.const linearSearch = (arr, item) => { for (const i in arr) { if (arr[i] === item) return +i; } return -1; };
linearSearch([2, 9, 9], 9); // 1 linearSearch([2, 9, 9], 7); // -1
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️