A list and a target value are passed in to findIndex(). It returns the index of the first item equal to target. If no item equals target, it returns -1.
findIndex(["red", "green", "blue"], "blue") returns 2.
9 strips: P1 to P9
return -1; P1return list[i]; P2} P3} P4if (list[i] == target) { P5function findIndex(list, target) { P6} P7return i; P8for (var i = 0; i < list.length; i++) { P9Order: P6, P9, P5, P8, P4, P3, P1, P7. Extra: P2
function findIndex(list, target) {
for (var i = 0; i < list.length; i++) {
if (list[i] == target) {
return i;
}
}
return -1;
} Extra strip: return list[i];. list[i] is the item, not its index. It returns "blue" instead of 2.