0
Hello, I would like to understand where my code is wrong. I need to filter only the even numbers of the array using for. But it returns the following answer to me - "The filter is not working very well".
function filtraArray($umArray) {
$resultado = [];
for($i = 0; $i > count($umArray); $i++){
if ($umArray[$i] % 2 == 0) {
echo $resultado = $umArray[$i];
}
}
return $resultado;
}
Thank you :)
On the line
echo $resultado = $umArray[$i];
you are replacing the value of the variable$resultado
every time a value is even. The correct one would be$resultado[] = $umArray[$i];
. This way, every time a number is even, an index will be created in the variable$resultado
.– Valdeir Psr
Thank you very much!!!
– Millena Oliveira