# Credit Card Challenge in Javascript Syntax Part 2 does not return valid on all valid cards

I’m new to studying Javascript and I’ve just got stuck on this project. My code only shows some of the valid numbers as valid.

``````// Add your functions below:
const validateCred = array => {
const newArr = array.reverse();
let check = newArr.shift();
let doubledSum = 0;
let singleSum = 0;
for (let i=1; i<newArr.length; i+= 2) {
if ((newArr[i]*2) > 9){
doubledSum += ((newArr[i]*2)-9);
} else {
doubledSum += newArr[i]*2;
}
}
for (let j =0; j< newArr.length; j+=2) {
singleSum += newArr[j];

}
const result = (singleSum + doubledSum + check) % 10;
if (result === 0) {
console.log('valid');
} else {
console.log('invalid');
}

}
validateCred([5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9]);
// This is supposed to be a valid card number but it is logging invalid to the console.type or paste code here
``````

Hi,
a few observations:

This causes the original array (credit card number) to be reversed because it doesn’t make a copy of the array. That will cause trouble when you will continue with the other functions to follow.

Same thing here: It will alter the original array passed into the function.

Since you stripped the check digit from the array, and if I remember correctly, you should double every other digit starting with the number right next to the check digit, this for loop should start at 0 and the other one should start at 1 instead.

So if you swap the starting numbers of the two for loops and make a real copy of the array like this for example:

``````const newArr = [...array];
newArr.reverse();
``````

You should be done with the 1st function.

1 Like

Thank you! I can see now where I was wrong. Now it works out like it should.

1 Like