...

/

Solution Review: Count Even Integers in a List

Solution Review: Count Even Integers in a List

This lesson explains the solution for the count even integers in a list.

We'll cover the following...

Solution

Press + to interact
let list = [10, 3, 17, 23, 40, 22, 99];
let rec countEven = (myList) => {
switch(myList) {
| [] => 0 /* Base case -> Return 0 */
| [head, ...tail] => {
if(head mod 2 == 0){
1 + countEven(tail); /* recursive call with increment if head is even */
} else {
countEven(tail); /* Recursive call without increment */
}
}
};
};
Js.log(countEven(list));

Explanation

The solution relies heavily on recursion. ...