Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
array.map is not a function even though I have it typed correctly

I am wondering why I'm getting this error message? But array.map is correct in how I'm stating it?

/home/ccuser/workspace/credit-card-checker/main.js:40 newArr = array.map((element, index) => index % 2 === 1 ? element * 2 : element); ^

TypeError: array.map is not a function at validateCred

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

const validateCred = (array) => {
  let newArr = [];
  let newestArr = [];
  let reducedResult = 0;
  const reducer = (previousValue, currentValue) => previousValue + currentValue;
  if (array.length % 2 === 0) {

    newArr = array.map((element, index) => index % 2 === 0 ? element * 2 : element);
  } else {
    newArr = array.map((element, index) => index % 2 === 1 ? element * 2 : element);
  }
  newestArr = newArr.map((element) => element > 9 ? element - 9 : element);

  let sum = newestArr.reduce((prev, curr) => prev += curr);
  return sum % 10 === 0
};

const findInvalidCards = (nestedArray) => {
  let invalidArray = [];
  invalidArray = nestedArray.forEach((element) => validateCred(element) === false);
  return invalidArray;
};

findInvalidCards(valid1);

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

valid1 is supposed to be an array of arrays, so here I just wrap another set of square brackets.

Also, forEach always returns undefined, so I changed it to use map instead.

const valid1 = [[4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]];

const validateCred = (array) => {
  let newArr = [];
  let newestArr = [];
  let reducedResult = 0;
  const reducer = (previousValue, currentValue) => previousValue + currentValue;
  if (array.length % 2 === 0) {

    newArr = array.map((element, index) => index % 2 === 0 ? element * 2 : element);
  } else {
    newArr = array.map((element, index) => index % 2 === 1 ? element * 2 : element);
  }
  newestArr = newArr.map((element) => element > 9 ? element - 9 : element);

  let sum = newestArr.reduce((prev, curr) => prev += curr);
  return sum % 10 === 0
};

const findInvalidCards = (nestedArray) => {
  let invalidArray = [];
  invalidArray = nestedArray.map((element) => ({cardNumber: element.join(""), isValid: validateCred(element)}));
  return invalidArray;
};

console.log(findInvalidCards(valid1));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda