Check which array only has even numbers

Asked

Viewed 1,698 times

-1

Write a function called getValidPassword receiving a two-dimensional array as a parameter.

Each array entry represents one passcode. You need to find the passcode that has no odd numbers and return it. Example:

var loggedPasscodes =[
    [1, 4, 4, 1],
    [1, 2, 3, 1],
    [2, 6, 0, 8],
    [5, 5, 5, 5],
    [4, 3, 4, 3]
];

getValidPassword(loggedPasscodes) // returns the array: [2, 6, 0, 8]

This is my resolution:

var getValidPassword = function (loggedPasscodes) {
    var passcode;

    for (var i = 0; loggedPasscodes.length > i; i++)	{
        for (var j = 0; loggedPasscodes[i].length > j; j++) {

            if(loggedPasscodes[i][j] % 2 == 0)	{ // even
             passcode = loggedPasscodes[i];
             break;
            }  
        }
    }

return passcode;

};

var 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));

Give me the result [4, 3, 4, 3], unfortunately.

2 answers

1


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]]

-1

var getValidPassword = function (loggedPasscodes) {
    var passcode, hasOdd;

    for (var i = 0; loggedPasscodes.length > i; i++)    {
        hasOdd = false;

        for (var j = 0; loggedPasscodes[i].length > j; j++) {

            if(loggedPasscodes[i][j] % 2 !== 0)  { // odd
                hasOdd = true;
                break;
            }  
        }

        if (!hasOdd) {
            passcode = loggedPasscodes[i];
            break;
        }
    }

    return passcode;
};

var 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));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.