All puzzles
Per page
O
Return true or false ยท One function calls another

Count the even numbers

Write isEven() first, then countEvens(). A number is passed in to isEven(), and it returns true when the number is even. A list is passed in to countEvens(). It uses isEven() to count the even numbers in the list, and returns the count.

countEvens([4, 8, 10, 7]) returns 3.

16 strips: O1 to O16

if (isEven(list[i])) {
O1
}
O2
if (isEven(i)) {
O3
}
O4
}
O5
}
O6
return count;
O7
function isEven(n) {
O8
return false;
O9
for (var i = 0; i < list.length; i++) {
O10
if (n % 2 == 0) {
O11
function countEvens(list) {
O12
}
O13
return true;
O14
var count = 0;
O15
count = count + 1;
O16

Answer key: O. Count the even numbers

Order: O8, O11, O14, O2, O9, O4, O12, O15, O10, O1, O16, O6, O5, O7, O13. Extra: O3

function isEven(n) {
	if (n % 2 == 0) {
		return true;
	}
	return false;
}
function countEvens(list) {
	var count = 0;
	for (var i = 0; i < list.length; i++) {
		if (isEven(list[i])) {
			count = count + 1;
		}
	}
	return count;
}

Extra strip: if (isEven(i)) {. i is the index, not the number. This counts the even indexes, 0 and 2, so for [4, 8, 10, 7] it returns 2.