# Credit Card Checker Project - Keeps Returning False

Here’s a link to the exercise I’m trying to work through: Credit Card Checker | Codecademy

I’m trying to solve the Credit Card Checker project on the Full-Stack Engineer Path, but keep getting ‘false’ as my return for both valid and invalid credit card numbers. I’m following the hint given to use the Free Formatter’s Luhn algorithm implementation, but I’m not sure where I’m going wrong. I tried logging the newArr[i] values as well as the sum values and they seem fine, but maybe I’m missing something?

Any help would be greatly appreciated. Thank you!

``````function validateCred(array) {
let newArr = array.slice(0, array.length-1);
const arrEndNum = array[array.length-1];
newArr = newArr.reverse();

for(let i = 0; i < newArr.length; i++) {
if(i % 2 === 1) {
newArr[i] *= 2;
}
if(newArr[i] > 9) {
newArr[i] -= 9;
}
}

let sum = arrEndNum;
for(let i = 0; i < newArr.length; i++) {
sum += newArr[i];
}

if(sum % 10 === 0) {
return true;
} else {
return false;
}
}
``````

You’re not doing anything with these array item you increase or shrink. The original array `newArray` stays unchanged. You need to push (splice) them back into the array.

Hi @mirja_t

Thanks for the tip! I tried making a new array (arrToReturn) and pushing the new values to it, but still no dice. I still keep getting false no matter which array I use to test it.

``````if(i % 2 === 1) {
newArr[i] *= 2;
}
if(newArr[i] > 9) {
newArr[i] -= 9;
}

arrToReturn.push(newArr[i]);
``````

You push the unaltered number, because you’re doing it outside of the if statements. Put a `console.log(newArr[i])` below the line where you push the number to see which number you’re pushing there.

Edit: Sorry, the above explanation wasn’t correct. You do alter the number. But once you alter it in the first if statement, it might also pass the second condition, because the altered number is passed to that condition, which isn’t what you intend.
So what you should do is cache the number in a new variable and push that variable. In order for that to be correct, you need to work with else if and else conditions.

Summary
``````let cache = 0;
for(let i = 0; i < newArr.length; i++) {
if(newArr[i] % 2 === 1) {
cache = newArr[i] * 2;
}

else if(newArr[i] > 9) {
cache = newArr[i] - 9;
}
else {
cache = newArr[i];
}

arrToReturn.push(cache);
}
console.log(newArr); // output: [0, 8, 6, 1, 0, 8, 0, 9, 7, 7, 6, 9, 3, 5, 4]
console.log(arrToReturn); // output: [0, 8, 6, 2, 0, 8, 0, 18, 14, 14, 6, 18, 6, 10, 4]
``````

Also have a look at the consecutive statement.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.