Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question