Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!