Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Exercise - find a passcode using dimensional arrays and function

Bendito

Coder
Hi!
I´m stuck on this:

Write a function named getValidPassword that takes a two dimensional array as parameter.

Each entry in the first array represents a passcode. You need to find the passcode that has no odd digits and returns that passcode from your function.
Here's an 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]

Tried this way, but error message shows: >>>>Code is incorrect Function getValidPassword is not working as requested." [2,6,0,8].

Code:
`var loggedPasscodes=[
    [1, 4, 4, 1],
[1, 2, 3, 1],
[2, 6, 0, 8],
[5, 5, 5, 5],
[4, 3, 4, 3],
];
var getValidPassword = function(getPassword){
var passcode = [];
var cache = [];
for (var i = 0; i < getPassword.length; i++){
for (var j = 0; j < getPassword[i].length; j++){
if(getPassword[i][j] % 2 === 0){           cache.push(getPassword[i][j]);
}
else {break;
}
if(getPassword[i].length === cache.length){
passcode= cache.slice();
}
}
}
return passcode;
};
console.log('[' + getValidPassword(loggedPasscodes) + ('') + ']');`

Also so this:
Code:
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));

and

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

But none of the above are working. Can you please help me, I´m almost finnished all the exercices.

Thanks
 
Hi!
I´m stuck on this:

Write a function named getValidPassword that takes a two dimensional array as parameter.

Each entry in the first array represents a passcode. You need to find the passcode that has no odd digits and returns that passcode from your function.
Here's an 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]

Tried this way, but error message shows: >>>>Code is incorrect Function getValidPassword is not working as requested." [2,6,0,8].

Code:
`var loggedPasscodes=[
    [1, 4, 4, 1],
[1, 2, 3, 1],
[2, 6, 0, 8],
[5, 5, 5, 5],
[4, 3, 4, 3],
];
var getValidPassword = function(getPassword){
var passcode = [];
var cache = [];
for (var i = 0; i < getPassword.length; i++){
for (var j = 0; j < getPassword[i].length; j++){
if(getPassword[i][j] % 2 === 0){           cache.push(getPassword[i][j]);
}
else {break;
}
if(getPassword[i].length === cache.length){
passcode= cache.slice();
}
}
}
return passcode;
};
console.log('[' + getValidPassword(loggedPasscodes) + ('') + ']');`

Also so this:
Code:
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));

and

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

But none of the above are working. Can you please help me, I´m almost finnished all the exercices.

Thanks
Hi! Could this help?

var loggedPasscodes = [
[1, 4, 4, 1],
[1, 2, 3, 1],
[2, 6, 0, 8],
[5, 5, 5, 5],
[4, 3, 4, 3]
];
var getValidPassword = function(loggedPasscodes) {
for (var i = 0; i < loggedPasscodes.length; i++) {
for (var j = 0; j < loggedPasscodes.length; j++) {
if ((loggedPasscodes[j]%2) !==0) {
break;
}
return loggedPasscodes;
}
}
};
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom