The main problem in your code is the comparison of arrays. You should not compare arrays using ==
, because the result is not quite what you expect. For example, if I compare []
with []
, should be considered equal? Javascript does not think:
console.log([] == []); // false
That’s because the operator ==
is following the rules of Abstract Equality Comparison Algorithm. The point that interests us about this algorithm, which appears in the documentation, is:
If the operands are Both Objects, Return true only if Both operands Reference the same Object.
In free translation:
If operands are objects, returns true only if they both reference the same object.
And how arrays are objects (inclusive, typeof([])
returns object
), and []
always creates a new array, its comparison if (valido != [])
will always be true
(no matter if valido
is empty or not). And if the array valido
is empty, the value of valido[0]
will be undefined
, because it is empty, there is no element at zero position (which is what happens when no number is found).
That said, I think you’re complicating the algorithm for nothing. If the idea is to return the first value found, forget this array valido
and use a return
within the for
:
function buscarDivisivel(array, num) {
for (var i = 0; i < array.length; i++){
if (array[i] % num == 0 && array[i] != 0){
return array[i];
}
}
// se chegou aqui, é porque nenhum número foi encontrado
return "Nenhum número foi encontrado!";
}
var array = [10, 4, 7, 128, 42, -1, 0, 300, -5];
var num = 400;
console.log(buscarDivisivel(array, num)); // Nenhum número foi encontrado!
console.log(buscarDivisivel(array, 100)); // 300
That is, if the number is found, return it (and the return
returns the value immediately, interrupting the for
and the function itself, that is, all the rest will not be executed). If none is found, it never enters the if
, the for
ends and it returns the message.
Although I particularly do not think it very good to return a number or error message.
If no number is found, it would be better to return something like undefined
, for example, to indicate that nothing was found. Then who called the function check the return and do what you want in case of error (can even print the message, if applicable):
function buscarDivisivel(array, num){
for (const n of array) { // só para mostrar outra forma de fazer o *loop*
if (n % num == 0 && n != 0){
return n;
}
}
// se chegou aqui é porque não encontrou nada
return undefined;
}
let array = [10, 4, 7, 128, 42, -1, 0, 300, -5];
let num = 400;
let n = buscarDivisivel(array, num);
if (n !== undefined) {
console.log(n);
} else {
// não encontrou nada, imprime a mensagem
// (mas eu poderia fazer qualquer outra ação, sem depender do retorno da função)
console.log('Nenhum número foi encontrado');
}
By the way, this approach of returning undefined
is exactly what the method find
makes (which was one of the suggestions of another answer). Then it would look like this:
let array = [10, 4, 7, 128, 42, -1, 0, 300, -5];
let num = 400;
let n = array.find(n => n % num == 0 && n != 0);
if (n !== undefined) {
console.log(n);
} else {
console.log('Nenhum número foi encontrado');
}
Even if we look at language specification, we will see that find
does basically the same as its function: traverses the array and returns the first element that satisfies the condition (or undefined
, if none is found).
Of course the fact that you have to call one callback for each element of the array find
be a little slower, but for small arrays it won’t make that much difference.
Finally, in the examples above I also put semicolon at the end of the lines. It may sound "fresh," and I know that Javascript "accepts" the semicolon code and "works," but it avoids some bizarre situations that can occur if you don’t use them, like that one and that one (see more about this here).
And I disagree with another answer in relation to for
with break
or return
be something that should not be used. All right that runs outside the scope of the question, but for a more in-depth discussion, I suggest reading here, here and here.
Thank you. It helped a lot. Now I know pq the function returned "Undefined". Just one more question. If I want to compare two arrays, what’s the best way to do this in JS ? Since if I do console.log([1,2] == [1,2]) it will return false.
– Lucas ribeiro
@Lucasribeiro To compare arrays in this way, you have to go through them and compare element by element: https://stackoverflow.com/a/14853974
– hkotsubo