Challenge: Lambda Expression
Test your understanding by solving the exercise given in this lesson.
We'll cover the following
Problem statement
Given a list of integers, count the number of occurrences of even numbers in the list. To do this, you are required to perform the following tasks:
- Define a function,
countOfEven
, to calculate the number of even integers in the input list,listOfIntegers
, of theList<Int>
type, passed to the function. - One of the ways to loop over a list in Kotlin, is to invoke the higher order function
forEach
on that list. Pass a lambda expression toforEach
.
Sample test case
var listOfIntegers = listOf(1, 4, 6, 3, 9, 7, 0)
countOfEven(listOfIntegers)
Expected output
3
Get hands-on with 1400+ tech skills courses.