Why does this function affect the result of my other function?

Link to ‘Credit Card Checker’ project:

I am currently working on the ‘credit card checker’ project and I am on project requirement 4 which is to create the ‘.findInvalidCards()’ function. After typing out my code for the function and running the project I’ve noticed a couple of problems:

The first is that my function for requirement 3, ‘.validateCred()’, is no longer working properly and is returning incorrect results. I know the issue comes from the ‘.findInvalidCards()’ function because when I comment that function out, the ‘.validateCred()’ function returns the correct results again.

The second problem is that I’ve noticed my code for ‘.findInvalidCards()’ is not working as intended and is returning brand new arrays instead of filtering out the valid credit card arrays.

The code I have written for the project is below:

// Add your functions below:

const validateCred = array => {

let tempArray = array;

for(let i = array.length - 2; i >= 0; i -= 2) {

tempArray[i] *= 2;

if(tempArray[i] > 9)

  tempArray[i] -= 9;


let num = 0;

tempArray.forEach(number => num += number);

if((num % 10) === 0)

return true;

return false;


const findInvalidCards = array => {

const invalidArray = array.filter(arr => validateCred(arr) === false);

return invalidArray;







I see a potential problem in the validateCred function here:

You created tempArray and then manipulated the stuff in tempArray to check if the card is valid.

However, this does not copy the array - it just creates a reference to the array that happens to be an argument/parameter of the function. [It just copies the memory address of the array.]
So changing stuff in tempArray also changes the stuff inside whatever array this function is called on.
(This happens because an array is a kind of object, not a primitive type in JavaScript.)

One way to get around this issue is to use Array.from() to build a new array.

let tempArray = Array.from(array);

Answer previously posted: