Lesson 1: Arrays and linear search
An array stores an ordered sequence. Linear search checks values one by one until it finds the target or reaches the end.
function findIndex(values, target) {
for (let i = 0; i < values.length; i += 1) {
if (values[i] === target) return i;
}
return -1;
}
findIndex([4, 9, 12], 9); // 1In the worst case, the algorithm inspects every item, so its running time grows linearly with list size: O(n). It uses constant extra space: O(1).
Practice
Update the function to count how many times a target appears. Test an empty array, a missing value, and repeated values.