 # Credit Card Checker - lost the numbers?

Hi, I’ve been stuck on the Credit Card Checker project for a little while now. I thought I was getting closer (it’s finally returning true when I check a valid card number!), but when I console.log() to check my work, all it shows is 0. Not really sure what I’m doing wrong. Could someone please check my code and tell me what I’m missing?

``````const validateCred = digitArray => {
let firstSum = 0;
let skipped = 0;
for (let i = digitArray - 2; i > 0; i-=2) {
let doubled = i * 2;
if (doubled > 9) {
doubled -= 9;
firstSum += doubled;
}
}
//console.log(firstSum);
for (let j = digitArray - 1; j > 0; j-=2){
skipped += j;
}
//console.log(skipped);
let finalSum = firstSum + skipped;
//console.log(finalSum);
if (finalSum % 10 === 0) {
return true;
} else {
return false;
}
}

console.log(validateCred(valid1));
``````

Thank you!

Hi,
Without knowing your input I can only make assumptions. Is `valid1` an array of digits?
If so, both your loops have similar types of declarations: `let i = digitArray - 2`.
Try logging what `digitArray-2` returns.

1 Like

Yes, sorry. Should have included that. valid1 is an array of 16 digits, meant to represent a credit card number.

It logs 0.

No problem!

So what I’m trying to get that is you have a loop that iterates down from a higher index. You’re not actually getting the length of the digitArray yet so those loops aren’t going to run properly. (as a test, console.log i or j inside the loops to confirm) I forgot to use .length().

Happens to the best of us!

But if I’m inputting this array:

``````const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
``````

why is my code using the numbers 1-16 instead of the numbers in the array?

``````const validateCred = array => {
let firstSum = 0;
let skipped = 0;
for (let i = array.length - 2; i > 0; i-=2) {
console.log(i);
let doubled = i * 2;
if (doubled > 9) {
doubled -= 9;
firstSum += doubled;
//console.log(firstSum);
}
}
//console.log(firstSum);
for (let j = array.length - 1; j > 0; j-=2){
//console.log(j);
skipped += j;
}
//console.log(skipped);
let finalSum = firstSum + skipped;
//console.log(finalSum);
if (finalSum % 10 === 0) {
return true;
} else {
return false;
}
}

console.log(validateCred(valid1));
``````

Well, the loop only knows the number range of the indices, so it is iterating through an index list, and not the values themselves. When the values are needed, you can simply use the accessor `[]`. (e.g.: `array[i]`). It’s a little bit of extra calculation, but it can be very useful for certain algorithms.

1 Like

It worked!!!

``````const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

const validateCred = array => {
let firstSum = 0;
let skipped = 0;
for (let i = array.length - 2; i >= 0; i-=2) {
//console.log(array[i]);
let doubled = array[i] * 2;
if (doubled > 9) {
doubled -= 9;
}
firstSum += doubled;
//console.log(firstSum);
}
for (let j = array.length - 1; j >= 0; j-=2){
//console.log(array[j]);
skipped += array[j];
}
//console.log(skipped);
let finalSum = firstSum + skipped;
console.log(finalSum);
if (finalSum % 10 === 0) {
return true;
} else {
return false;
}
}

console.log(validateCred(valid1));
``````

Thank you so much!!!

1 Like