Credit Card Checker findInvalidCards

Hi there!
Hope someone can help explain my error message. When I run my findInvalidCards function, I’m getting an error from my previous validateCred function, even though it works. Please see my uploaded files.

Thank you!

Hello, @css4602795524. Welcome to the forums!

Could you explain the following line from your code to me?

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

Hi, I need help with the first few steps validating credit card numbers.
My code starts with copying the credit card number array to a new array (in order to not mutate the original). I then loop through it from the 2nd item from the end, and run the steps of Luhn Algorithm.
However, testing the code, I do not get consistently true for the valid numbers, and not false for the invalid numbers. I cannot figure out what the mistake is:

const validateCred = (array) => {
let testArray = array;
for (let i = testArray.length - 2; i >= 0; i -= 2) {
testArray[i] = testArray[i] * 2;
if (testArray[i] > 9) {
testArray[i] = testArray[i] - 9;
}
console.log(testArray);
let arraySum = testArray.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
});
return arraySum % 10 != 0 ? console.log(‘false’) : console.log(‘true’);
}
}

validateCred(valid1);

// Output for valid1 number array:
[ 4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8 ]
false

Thank you for your halp!

Thanks for the reply!
I wanted to get both the valid and invalid numbers, but I decided to remove, if (arr[i] === validateCred(true)). I’ve changed my code, but still seeing the error.

So, now the line is:
if (nestArr[i] === validateCred(false))

I think you have mixed things up a little bit. validateCred() is a function. From the name of the parameter it accepts, I would think you mean to pass an array as the argument. In the function call validateCred(false) what are you passing as the argument?

oh I think I see what you mean! I didn’t put an argument (arr) to be used with the validateCred function. Is that correct? I replaced it with, if (validateCred(nestArr[i]) === false) and it looks like it worked!

1 Like

There you go. Happy coding!

Hello, @mega1585133659.

You have a few issues here. First, let testArray = array doesn’t copy the array. It assigns a second variable to the same array. Regardless of which name (variable) you use the original array will be mutated. There are a few ways to make a copy of an array. One is the Array.slice() method.

Beyond that look closely at how your code is organized. It seems you may have a few things inside your for loop that you probably don’t want in there.

1 Like

Thank you so much for your teaching and explanation. It’s the first time I understand that the equation would not copy the array as a whole. I’m scrapped it and start again.
I have a question:
Thanks!

1 Like