# Challenge Project: Credit Card Checker - Need help understanding the Code Solution

Hi guys! I’ve been really struggling with the Challenge Project: Credit Card Checker and had to download the code solution to try to understand how to complete this project.

The first task is to Create a function, `validateCred()` that has a parameter of an array. The purpose of `validateCred()` is to return `true` when an array contains digits of a valid credit card number and `false` when it is invalid. This function should NOT mutate the values of the original array.

To find out if a credit card number is valid or not, use the Luhn algorithm. which is cone by following these steps.

1. Starting from the farthest digit to the right, AKA the check digit, iterate to the left.
2. 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.
3. Sum up all the digits in the credit card number.
4. 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.

Looking at the first function in the codes solution i am able to understand what the code is doing, and here is the first function…

``````function validateCred(numArr) {
let total = 0;
for (let i = numArr.length - 1; i >= 0; i--) {
let currValue = numArr[i]
if ((numArr.length - 1 - i) % 2 === 1) {
currValue *= 2;
if (currValue > 9) {
currValue -= 9;
}
}
total += currValue;
}

}
``````

The one line in this function that i don’t understand is…

``````    if ((numArr.length - 1 - i) % 2 === 1) {
``````

I understand that it is selecting all the values from the array except the last and is selecting every second array element but don’t understand how it’s doing it? The modular part confuses me?

If anyone could elaborate more on what’s going on here it would be much appreciated.

The modulo operator gives you the rest of a division.
If you have `5 % 2` it returns 1 because the rest of the division is 1:
5 / 2 = 2 (absolute number, rest 1)

``````(5 % 2 === 1)
``````

If you divide an even value with 2, the rest is 0, so it always gives you 0 for % 2 on even values.
If you divide an odd value with 2, the rest is 1, so it always gives you 1 for % 2 on even values.

Hi mirja_t, thanks for your response.

I understand what modular does, giving back the remainder value of the division, but i’m still confused as to what it is used for here as there are both odd and even numbers in the array of credit card numbers or is it check something else?

Can you please explain further, maybe explaining what the whole line of code is doing?

``````    if ((numArr.length - 1 - i) % 2 === 1) {
``````

Let’s say you have a credit card item like this:
`let numArr = [0,1,2,3,4]`

The for loop starts with the last digit of the array, which is 4 in our case:

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

Our credit card length is odd, so the modulo operation is 0 for the first round:
`(numArr.length - 1 - i) % 2` ( `(5 - 1 - 0) % 2 === 1 // false` )
This means that the first item is omitted and not doubled.

If it were even, it would start doubling the first digit in the array.
This way it is ensured that the check digit is not doubled, but the item next to it is.

1 Like

Hi again! That make’s sense and would mean that all the odd length arrays would not have their check digit double, but what about the arrays that have an even length? You said that “if it were even, it would start doubling the first digit in the array”.

But there are both odd and even array lengths in the project and they all omit the first item and the even length arrays do not start doubling the first digit? I’m confused

``````let odd = [0,1,2,3,4] // check digit: 4 – should not be doubled
let even = [0,1,2,3] // check digit: 3 – should not be doubled
``````
``````odd[5 - 1 - 0] //  4 even: not doubled
odd[5 - 1 - 1] //  3 odd: doubled
odd[5 - 1 - 2] //  2 even: not doubled
odd[5 - 1 - 3] //  1 odd: doubled
odd[5 - 1 - 4] //  0 zero: not doubled – check digit
``````
``````even[4 - 1 - 0] //  3 odd: doubled
even[4 - 1 - 1] //  2 even: not doubled
even[4 - 1 - 2] //  1 odd: doubled
even[4 - 1 - 3] //  0 zero: not doubled – check digit
``````

Thank you so much for breaking it down like that, I get it now!

After reviewing your breakdown once more i’m confused again as you state that ```
check digit: 3 – should not be doubled

``````but you do double it...

``````

even[4 - 1 - 0] // 3 odd: doubled
even[4 - 1 - 1] // 2 even: not doubled
even[4 - 1 - 2] // 1 odd: doubled
even[4 - 1 - 3] // 0 zero: not doubled – check digit

``````
After reviewing this i'm again confused?``````

`let odd`:
check digit value: 4
check digit index: 4
`odd[5 - 1 - 0] // 4 (value of the equation 5-1-0 === 4) even: not doubled`

`let even`:
check digit value: 3
check digit index: 3
`odd[4 - 1 - 0] // 3 (value of the equation 4-1-0 === 3) odd: doubled`

But i thought the check digit is not supposed to be doubled?

``````let odd = [0,1,2,3,4] // check digit: 4 – should not be doubled
let even = [0,1,2,3] // check digit: 3 – should not be doubled

``````

Yes, and it isn’t:

``````even[4 - 1 - 3] //  0 zero: not doubled – check digit
odd[5 - 1 - 4] //  0 zero: not doubled – check digit
``````

The equation in the condition `(numArr.length - 1 - i)` always evaluates to 0.
`0 % 2 === 1` is false. So it is not doubled.

Sorry for the mis understand i was confused from your previous example…

``````let odd = [0,1,2,3,4] // check digit: 4 – should not be doubled
let even = [0,1,2,3] // check digit: 3 – should not be doubled
odd[5 - 1 - 0] //  4 even: not doubled
odd[5 - 1 - 1] //  3 odd: doubled
odd[5 - 1 - 2] //  2 even: not doubled
odd[5 - 1 - 3] //  1 odd: doubled
odd[5 - 1 - 4] //  0 zero: not doubled – check digit
even[4 - 1 - 0] //  3 odd: doubled
even[4 - 1 - 1] //  2 even: not doubled
even[4 - 1 - 2] //  1 odd: doubled
even[4 - 1 - 3] //  0 zero: not doubled – check digit
``````

Just to clarify - So the above example should look like this…

``````let odd = [0,1,2,3,4] // check digit: 4 – should not be doubled
let even = [0,1,2,3] // check digit: 3 – should not be doubled
odd[5 - 1 - 4] //  4 even: not doubled
odd[5 - 1 - 3] //  3 odd: doubled
odd[5 - 1 - 2] //  2 even: not doubled
odd[5 - 1 - 1] //  1 odd: doubled
odd[5 - 1 - 0] //  0 zero: not doubled – check digit
even[4 - 1 - 3] //  3 odd: doubled
even[4 - 1 - 2] //  2 even: not doubled
even[4 - 1 - 1] //  1 odd: doubled
even[4 - 1 - 0] //  0 zero: not doubled – check digit
``````

Sorry i Meant this…

``````let odd = [0,1,2,3,4] // check digit: 4 – should not be doubled
let even = [0,1,2,3] // check digit: 3 – should not be doubled
odd[5 - 1 - 4] //  0 even: not doubled – check digit
odd[5 - 1 - 3] //  1 odd: doubled
odd[5 - 1 - 2] //  2 even: not doubled
odd[5 - 1 - 1] //  3 odd: doubled
odd[5 - 1 - 0] //  4 even: not doubled
even[4 - 1 - 3] //  0 even: not doubled  – check digit
even[4 - 1 - 2] //  1 odd: doubled
even[4 - 1 - 1] //  2 even: not doubled
even[4 - 1 - 0] //  3 odd: doubled
``````

Yes, you’re right. Sorry for the confusion. The order of execution is as in your example.

No worries, Thank you so much for you help!

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