The problem is you’re coming out of the second loop once you find an even number. That is, if you have an even number and you have some odd number afterwards, the odd number doesn’t even have a chance of being checked. And if you have any odd numbers before, it is being ignored. That is, you are not detecting if you have odd numbers.
And anyway, you’re always going through all the passcodes and returning the latter, regardless of whether it only has even numbers or not (make a table test and debug the code would help to understand these problems).
To know if all numbers are even, you should check if any are odd, and then yes you can interrupt the loop. And you also need to store the information that this odd number has been found, otherwise you will have no way of knowing if the passcode meets the condition of having only even numbers.
Another detail is that you don’t have to var nomeFuncao = function() {etc}
. There’s no point in doing this (and in that case it makes no difference). Do not invent and declare the function normally using function nomeFuncao() {etc}
:
function getValidPassword(loggedPasscodes) {
for (let i = 0; loggedPasscodes.length > i; i++) {
let passcode = loggedPasscodes[i];
let temImpar = false;
for (let j = 0; passcode.length > j; j++) {
if (passcode[j] % 2 !== 0) { // ímpar
temImpar = true;
// se encontrou um ímpar, sai do loop (não precisa verificar os demais números)
break;
}
}
if (! temImpar) {
return passcode; // não tem ímpar, pode retornar direto
}
}
};
let loggedPasscodes =[
[1, 4, 4, 1],
[1, 2, 3, 1],
[2, 6, 0, 8],
[5, 5, 5, 5],
[4, 3, 4, 3]
];
console.log(getValidPassword(loggedPasscodes)); // [2, 6, 0, 8]
The detail is that this code will find the first passcode that has only even numbers (if you have more than one). If you have none, the return is undefined
(and as the statement does not say what to do in this case, we can leave it anyway).
If you want all the passcodes that satisfy the condition (and not only the first), then you have to save them in an array and only at the end return this array:
function getValidPassword(loggedPasscodes) {
let codes = [];
for (let i = 0; loggedPasscodes.length > i; i++) {
let passcode = loggedPasscodes[i];
let temImpar = false;
for (let j = 0; passcode.length > j; j++) {
if (passcode[j] % 2 !== 0) { // ímpar
temImpar = true;
// se encontrou um ímpar, sai do loop (não precisa verificar os demais números)
break;
}
}
if (! temImpar) {
codes.push(passcode); // não tem ímpar, adiciona nos resultados
}
}
return codes;
};
let loggedPasscodes =[
[1, 4, 4, 1],
[1, 2, 3, 1],
[2, 6, 0, 8],
[5, 5, 5, 5],
[10, 2, 4, 42],
[4, 3, 4, 3]
];
console.log(getValidPassword(loggedPasscodes)); // [ [2, 6, 0, 8], [10, 2, 4, 42]]
Another alternative is to use the methods find
and every
:
function getValidPassword(loggedPasscodes) {
return loggedPasscodes.find(passcode => passcode.every(n => n % 2 === 0));
};
let loggedPasscodes =[
[1, 4, 4, 1],
[1, 2, 3, 1],
[2, 6, 0, 8],
[5, 5, 5, 5],
[4, 3, 4, 3]
];
console.log(getValidPassword(loggedPasscodes)); // [2, 6, 0, 8]
find
returns the first element of the array loggedPasscodes
satisfies the informed condition (or undefined
if none is found). In this case, the condition is that all numbers of passcode
are even (is what every
checks, returning true
if all meet the condition - if you have an odd number, the return is false
, indicating that the passcode does not satisfy the condition).
If you want all the passcodes that satisfy the condition (and not only the first one), simply change find
for filter
, which will return an array containing all the passcodes that satisfy the condition (or an empty array, if none is found):
function getValidPassword(loggedPasscodes) {
return loggedPasscodes.filter(passcode => passcode.every(n => n % 2 === 0));
};
let loggedPasscodes =[
[1, 4, 4, 1],
[1, 2, 3, 1],
[2, 6, 0, 8],
[5, 5, 5, 5],
[10, 2, 4, 42],
[4, 3, 4, 3]
];
console.log(getValidPassword(loggedPasscodes)); // [ [2, 6, 0, 8], [10, 2, 4, 42]]