I'm trying to check the contents of a list to see if an element is even or not. Here is the code:
def listEvenChecker2(aList: List[Int]): Boolean =
{
for (elem <- aList)
{
if (elem % 2 == 0)
{
return true
}
else
{
return false
}
}
}
val myList = List(1,2,3,4,5,6,7)
println(listEvenChecker2(myList))
Why am I getting this error:
If you what to check whether there is at least one even value, use exists
:
def listEvenChecker2(aList: List[Int]): Boolean =
aList.exists(_%2 == 0)
If you want to check that they are all even, use forall
:
def listEvenChecker2(aList: List[Int]): Boolean =
aList.forall(_%2 == 0)