1
Good night!
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, performed on the Chrome console:
When all numbers are even.
Parouimpar([6,6,6]); "True"
When all numbers are odd.
Parouimpar([1,1,1,1,1]); "False"
When a single number is even and should appear true.
Parouimpar([16,1,1,1,1, 1]); "False"
When a single odd number exists and should appear true, for all others are pairs.
Parouimpar([6,6,6,7]); "False"
When the last and first number of the vector are even.
Parouimpar([16,1,1,1,1, 16]); "True"
I understood that the function needs to analyze the whole vector and then check if it has at least one number to and return true or false, but I am not able to encode it.
Can anyone help me? Thank you very much!
It worked out! Thank you so much!
– Van Ribeiro