Credit Card Checker - original array modified?

I’m stuck with this project.

My code modifies the original (input) array.

Can someone please tell me why and what I am doing wrong.

Each time I call validateCred(valid1), valid1.length is reduced by one.

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const validateCred = (cardNumArr) => {
let inputArr = cardNumArr;
let popNum = inputArr.pop();
let reverseArr = inputArr.reverse();
let checkArr = ;
for (let i=0; i < reverseArr.length; i++) {
if (i % 2 === 0) {
if (reverseArr[i] * 2 > 9) {
checkArr.push(reverseArr[i] * 2 - 9);
} else {
checkArr.push(reverseArr[i] * 2);
}
} else {
checkArr.push(reverseArr[i]);
}
}
checkArr.push(popNum);
checkArr = checkArr.reduce((total, num) => {
return total + num;
});
return (checkArr % 10 === 0);
};

this:

let inputArr = cardNumArr;

does not create a copy of the array. Now we have simply two variables pointing to the same array in memory

think of it this way: your house (the array), if you give your address to two friends (variables), they now both can find your house, but any modifications made to the house (array) will be visible by both friends. Given its the same house on the same address

1 Like

YOU ARE OFFICIALLY MY HERO.

Changed “let inputArr = cardNumArr;” to “let inputArr = cardNumArr.map(x => x)” and now it works.

Thank you!!