Home > Net >  Exercise - find a passcode using dimensional arrays and function
Exercise - find a passcode using dimensional arrays and function

Time:12-17

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

`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 this:

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


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;
                 números)
                break;
            }
        }
        if (! temImpar) {
            return passcode; 
    }
};



 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

]

Also this:

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;
           
            break;
        }
    }
    if (! temImpar) {
        codes.push(passcode); 
    }
}
return codes;
};

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

This one:

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]

Also try to replicate something like this example:

function retornaNNumerosPares(n) {
let numerosPares = [];
for (let i = 0; numerosPares.length < n; i  ) {
    if (i % 2 == 0) {
        numerosPares.push(i);
    }
}
return numerosPares;

}

console.log(retornaNNumerosPares(5));

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

Can you please help?

CodePudding user response:

A fast approach takes every chance to end an unnecessary loop.

This approach iterate the array of codes and iterates the code as well by check unwanted numbers, then perform a continue of the outer loop, otherwise return the found array at the end of the inner loop.

const
    getValidPassword = codes => {
        outer: for (const code of codes) {
            for (const value of code) if (value % 2) continue outer;
            return code;
        }
    },
    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));

CodePudding user response:

I'd start by checking the first value of each array to knock out the uninteresting values. Any matches do my second loop and upon any detection it's even cancel out.

If a complete match is made you'd then return the result as show in comment or push it and continue finding more codes.

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

function getValidPassword(loggedPasscodes) {
  let Codes = [];
  for (let i = 0; i < loggedPasscodes.length; i  ) {
    console.log("Read Array:", i);
    if (loggedPasscodes[i][0] % 2 == 0) { // Check if first value is even
      let GoodCode = true;
      for (let x = 1; x < 4; x  ) { // Let's fast check the array now
        if (loggedPasscodes[i][x] % 2 != 0) {
          GoodCode = false;
          break; // Break if it's a waste of time (odds)
        }
      }
      //if (GoodCode == true) Codes.push(loggedPasscodes[i]); // Build our array if there's more codes even.
      if (GoodCode == true) return loggedPasscodes[i];
    }
  }
  return Codes;
};
console.log(getValidPassword(loggedPasscodes)); // [2, 6, 0, 8]

  • Related