E
E
Evtera2020-01-30 17:08:50
JavaScript
Evtera, 2020-01-30 17:08:50

How to pass nested arrays to a function containing loop?

Hello. The code below, under the last spoiler (the main code block), runs through the array with numbers and outputs a certain number. The crux of the matter is that there is an array parent

spoiler
const batch = [ valid1,valid2,valid3, valid4, valid5,];
, whose elements are only other arrays, which in turn already contain numbers.
spoiler
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

How can I send all the elements of the parent array to the validateCred function through the new function? (main block at the end of the question)

Tried something like this,
spoiler
const findInvalidCards = batchArray => {
for (let i = 0; i < batchArray.length; i++) {
validateCred(batchArray[i])
console.log( "number = " + batchArray[i]);
}
};

but when tested, the code gets stuck on let numberReverse = number.reverse(); (3rd line of the validateCred function in the main block)

The main block of code that produces the desired number number
spoiler

const validateCred = number => {
  let numberReverse = number.reverse();
  let evenArray = []; // even numbers
  let oddArray = []; // odd numbers

<i>// sort index position by odd numbers</i> 
  for (let index = 0; index < numberReverse.length; index += 2) {
    oddArray.push(numberReverse[index]);
  }

<i>  // sort index position by even numbers</i>
  for (let indexEven = 1; indexEven < numberReverse.length; indexEven += 2) {
    evenArray.push(numberReverse[indexEven] * 2);
    <i>//take two-digit even numbers and subtract nine</i>
    for (let indexMinus = 0; indexMinus < evenArray.length; indexMinus++) {
      if (evenArray[indexMinus] >= 10) {
        evenArray[indexMinus] -= 9;
      }
    }
  }

 <i> //.concat even  and odd numbers</i>
  let totalArray = oddArray.concat(evenArray);

 <i> //.reduce totalArray after .concat</i>
  let final = totalArray.reduce(function(a, b) {
    return a + b;
  });

  return final;

};



Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question