Credit Card Checker Project

Hi all,

I am currently working on the following project: https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-challenge-project-credit-card-checker/projects/credit-card-checker.

I have written the following function for item 3, yet the boolean value returned is false regardless of whether I input valid or invalid arrays as arguments.

const validateCred = arr => {

let sum = 0;

for (let i = arr.length; i >= 0; i--) {

    let checkDigit = arr[i];

    

    if (i % 2 === 0) {

        checkDigit *= 2;

        if (checkDigit > 9) {

            checkDigit -= 9;

        }

        sum += checkDigit;

    }

}

return sum % 10 == 0;

}

 console.log(validateCred(invalid1)); //expected output false

console.log(validateCred(valid1)); // expected output true

Can anyone offer a hint as to where I’m going wrong here?

Thanks in advance :slight_smile:
Andy

1 Like

Small Hint: JavaScript array indexes are 0-based, so if you use the length as an index, it would be outside of the range and would be undefined

Preemptive hint after you fix the first issue and most cards are working

You may find that valid3 is troublesome. It has 15 digits instead of 16, so you’ll need to deal with that in order to “double and check if greater than 9” the right digits.

I’ve adjusted my code as follows:

const validateCred = arr => {

let sum = 0;

for (let i = arr.length - 1; i >= 0; i--) {

    let checkDigit = arr[i];

    if (i % 2 === 0) {

        checkDigit *= 2;

    }

    if (checkDigit > 9) {

            checkDigit -= 9;

        }

        sum += checkDigit;

}

if (sum % 10 === 0) {

  return true;

} else {

  return false

}

}

console.log(validateCred(valid3));  // Output false expected true

console.log(validateCred(valid1)); // Outputs expected result  true

I’m stuck on valid3 as you suggested I might be, not entirely sure of the logic here and this could be because Math has never been a particular strong point of mine. Any help would be greatly appreciated :slight_smile:

The issue is that valid3 is an American Express card and has only 15 digits, as opposed to the others that have 16 digits, so your strategy to determine if the double/check above 9 part needs to be run based on the index breaks down.

The last digit shouldn’t have the extra double/check above 9 step run on it. On a 15 digit card, the last index is 14, which passes your i % 2 === 0 condition since it’s even. It continues on after that because the number at index 13 should go through the process, but it doesn’t because it’s odd.

It works out fine for your 16 digit cards.

Click if you'd like a strategy for dealing with it

One thing you could do to adapt your existing code is keep track of which digit you’re on in a separate counter, then using that counter in the condition where you check if it’s even with % 2.

You could create a variable before the for loop with an initial value of 0, then at the very beginning of the for loop you could increment it. Now you’d be able to use that new variable in place of i in your i % 2 === 0 condition.

The first loop will always be an odd number (1) so the index of the last digit doesn’t matter, and the rest of it works out too.

Hey @system3199972996 it’s been a little while since you originally posted this question. Could you come back to let us know if the above reply helped you?