Good evening!
I need to create a function that accepts an array of numbers and returns true if it contains at least an even number and false , otherwise.
The code below even does this, SQN:
var ParOuImpar = function(num){
var i, condicao;
var True = "True", False = "False";
for(i=0; i<num.length; i++){
if ((num[i] % 2) == 0) {
condicao = True;
}
else if ((num[i] % 2) != 0) {
condicao = False;
}
}
return condicao;
};
Some tests on the Chrome console:
When all numbers are even.
ParOuPrim ([6,6,6]); "True"
When all numbers are odd
ParOuPlus ([1,1,1,1,1,1]); "False"
When a single number is even and should appear true .
ParOpPlus ([16,1,1,1,1,1]); "False"
When there is a unique odd number and it should appear true , since all others are even.
ParOuPrim ([6,6,6,7]); "False"
When the last and first number of the vector are even.
ParOuPrim ([16,1,1,1,1, 16]); "True"
I understood that the function needs to parse the entire vector and then check if it has at least one number to and return true or false, but I'm not sure how to code this.
Can anyone help me? Right away, thank you!