All puzzles
Per page
P
Linear search ยท Return inside a loop

Where is it?

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;
P1
return list[i];
P2
}
P3
}
P4
if (list[i] == target) {
P5
function findIndex(list, target) {
P6
}
P7
return i;
P8
for (var i = 0; i < list.length; i++) {
P9

Answer key: P. Where is it?

Order: 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.