Help needed with 'Credit Card Checker' project

Unfortunately, despite hours of pouring over and tweaking my code, I can’t seem to get past the first step of the ‘Credit Card Checker’ project. Perhaps I should take this as a sign from the computing Gods to give up on my dreams of becoming a developer :upside_down_face: Self-pity aside, I’ve looked at Codecademy’s solution code and tweaked my own code accordingly, and yet, my function still does not work. What am I missing?

const validateCred = (cardNumber) => {

    let total = 0;

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

        let currentValue = cardNumber[i];

        currentValue *= 2;

        if (currentValue > 9) {

            currentValue -= 9;

        }

        total += currentValue;

    }

    if (total % 10 === 0) {

        return 'Valid credit card number.';

    } else {

        return 'Invalid credit card number.'

    }

};

console.log(validateCred(valid1));

(‘valid1’ is a reference to one of the provided ‘valid’ arrays of credit card numbers, which I reference below)
[4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8] <=
However, regardless of whether my input is a valid or invalid credit card number, my function will only return ‘Invalid credit card number’. Any help figuring out WHY my ‘validateCred’ function isn’t working before I quit out of frustration would be GREATLY appreciated.

Your loop:

only deals with half of the stuff in the array. Specifically, i -= 2 will do every other index.

You could make another loop to add the remaining stuff in the array to the total.

Also, notice that you’re starting the loop with i = cardNumber.length
so on the first iteration of the loop,
cardNumber[i]
would be undefined.

I might be misunderstanding, but I set the loop to specifically iterate through every other index, as that is what the directions outlined (in order to satisfy the requirements of the Luhn Algorithm.) Perhaps I should have posted them. Specifically, they state:

-Starting from the farthest digit to the right, AKA the check digit, iterate to the left.
-As you iterate to the left, every other digit is doubled (the check digit is not doubled). If the number is greater than 9 after doubling, subtract 9 from its value.
-Sum up all the digits in the credit card number.
-If the sum modulo 10 is 0 (if the sum divided by 10 has a remainder of 0 ) then the number is valid, otherwise, it’s invalid

yes, you seem to have dealt with most of that in your code correctly.
However, you didn’t sum up all digits in the code above.
You did the sum of the digits at every other index (starting at the next-to-last one and going in reverse order) after the required modifications.

So you need to also add in the other digits too.
(One way to do that is to have an additional loop after the loop you already have, but there are many other alternatives.)

1 Like

i is initialized to a non-existing index, so the value is undefined. It also doesn’t take into account the length as odd or even.

1 Like

That makes sense! Thank you.