Credit card Checker

hey, I’m having trouble with some of the numbers being jumbled up.
the validateCard function seems to work and guess all the cards correctly but when I print the numbers in the array the numbers are jumbled:

const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];

// Add your functions below:

function validateCred(arr){

const newArray = arr.reverse();

 for (let i = 0; i < arr.length; i++){

   if(i % 2 !== 0) {   

     newArray[i] = newArray[i]*2;

     if(newArray[i] > 9){ newArray[i] = newArray[i]-9; }




let sum = newArray.reduce((accumulator, currentValue) => { return accumulator + currentValue;}, 0);


let cardIsReal = false;

if (sum % 10 === 0){cardIsReal = true;}

else {cardIsReal = false;}


return cardIsReal;


function findInvalidCards(arr){

const newArray =[]

 for (let i = 0; i < arr.length; i++){

   if(validateCred(arr[i]) === false){





return newArray;



Hey there bendoenton! I haven’t encountered this exercise yet but maybe I can help you.
I have a lot of questions, so beware :stuck_out_tongue: :

  • why don’t you use a space or a tab to indent the declarations inside your functions?
  • why do you expect validateCred() to receive an array as a parameter if findInvalidCards() calls it passing it only an element of an array? Is this element also an array?
  • Are the elements of batch numbers, correct?
  • Why do you use continue inside the for loop of validateCred() if the loop is already at the end?
  • Why inside findInvalidCards() some declarations don’t terminate with a semicolon?
  • Why do you keep if and else statement code blocks in the same line? Also why do you put a semicolon at the end of their code block inside and not outside?

This phenomenon is called pass by reference. (some credits to @eddyfletch :wink:)
When you call the function validateCred() in the if clause of findInvalidCards , the equations you do inside that function alter the original array passed in. You already realized that when you reversed the array digits order back in the findInvalidCards function. But the additions for the Luhn check also change the original array.


i thought because i saved the edited ones to a new variable inside the function that wouldn’t affect the originals.
can I not just reference back to the original consts somehow?

You could map the array like this:

const newArray = => el).reverse();

not really sure what im doing to be honest just feeling it out

1 Like

yess that solves the problem! thanks so map is non destructive?

1 Like

No, map() makes a copy.
From the MDN docs:

map does not mutate the array on which it is called

1 Like

I’m happy that you found your solution with .map()
My suggestion would have been about the logic, in detail I would have used a .forEach() on batch. For each element of batch I would have:

  • checked they’re numbers, they’re the supposed credit/debit card length and any other standard sanity check it should be done, if not communicate they’re simply not valid because some of those reasons
  • push the valid ones to a global variable array previously defined e.g. validCards
  • push the invalid ones to a global variable array previously defined e.g. invalidCards

I wonder now, if storing an array of invalid cards would actually be useful to speed up the sanity checks by checking if the current card was already checked before…

Cheers mate

1 Like

Another way to make a copy of an array is to use the Array.slice() method.
You can also chain reverse() or other methods:

const blah = [1, 2, 3, 4, 5];
const notBlah = blah.slice().reverse();
console.log(blah); //blah is not mutated


[ 1, 2, 3, 4, 5 ]
[ 5, 4, 3, 2, 1 ]