Given an array of random numbers. Return the number of even numbers in the array.
function calculateEvenNumbers(array $numbers): int {
$result = [];
foreach ($numbers as $number) {
if ($number % 2 === 0) {
$result[] = $number;
}
}
return count($result);
}
<?php
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
public function testThatEvenNumbersAreCalculatedCorrectly() {
$this->assertEquals(0, calculateEvenNumbers([]));
$this->assertEquals(2, calculateEvenNumbers([5, 7, 2, 4, 9]));
$this->assertEquals(3, calculateEvenNumbers([60, 180, 99, 100, 101, 103]));
$this->assertEquals(0, calculateEvenNumbers([99, 9, 1, 21, 33, 77, 57, 33, 91, 111, 23451]));
$this->assertEquals(6, calculateEvenNumbers([10_000_000, 90_000, 700_000_000, 2, 70, 44]));
}
}