Are the provided cards labelled correctly in Credit Card Checker?

Hello everyone,
I finished the Credit Card Checker project:
Although I think I implemented the Luhn algorithm as described, the second and third card labelled as valid cards (const valid2 and const valid3) return false. The rest return the expected result:

const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];

const validateCred = (array) => {

  const arr = array.map(el => el).reverse();
  const newArr = [];
  
  let cache = 0;
  for( let i = 0; i < arr.length; i++ ) {
  	if( i % 2 === 1 ) {
    	cache = arr[i] * 2;
      if( cache > 10 ) {
        cache = cache - 9;
      }
    }
    else {
    	cache = arr[i];
    }
    newArr.push(cache);
  }
  
  cache = newArr.reduce( (prev, curr) => prev + curr );
  console.log('original card number: ' + arr.reverse() + '\n processed card number: ' + newArr.reverse() + '\n sum: ' + cache);
// "original card number: 5,5,3,5,7,6,6,7,6,8,7,5,1,4,3,9
 // processed card number: 10,5,6,5,5,6,3,7,3,8,5,5,2,4,6,9
 // sum: 89"
  
  if (cache % 10 === 0) {
  	 return true;
  }
}
validateCred(valid2);

Here’s a link to the complete code:
https://jsfiddle.net/mirja_t/odtafjrs/5/
Are they labelled incorrectly or did I do something wrong?

Good day,

Yes, the cards are indeed labeled correctly, so there must be a bug within the code.

You may use the below link to confirm the validation of the card numbers if you’d like.
https://www.validcreditcardnumber.com

Hi @codedbydom93
thank you for the link, if I enter valid2 it indeed passes. Did you have a look at the code and have an idea where it’s gone wrong?

I did.

Have a closer look at your Cache loop, there is an issue in there that is causing the false returns, causing your issue with the Valid cards, but also with 2 Mystery cards.

If you need anymore help, let me know.

PS: It’s a small mistake.

1 Like

I think there is a little discrepancy between the graph and the description here:
https://content.codecademy.com/PRO/independent-practice-projects/credit-card-checker/diagrams/cc%20validator%20diagram%201.svg
What I understood was that I need to process every second digit. Is that the mistake?

The graph is correct, as well as the description.

The issue lies within the loop, think how a loop uses the ‘i’ variable.

Ok that’s sloppy :roll_eyes: : Each number bigger than 9, not 10 is supposed to be reduced by 9. Thank you for putting my attention to the for loop. All the returned values (at least from 1 to 10) are as expected now.

I still think there’s a little discrepancy between the text and the graph: In the text it says ‘every other digit is doubled (the check digit is not doubled)’ which I understand means that each digit except the last one is supposed to be doubled. The graph shows that just every second digit (from the right) is supposed to be doubled, which is what I did and what Wiki says is correct. Or is that just a linguistic misunderstanding?

Glad that you found the issue.

When it comes to for loops, when the ‘i’ value is 0, it still counts as a value, so going from 0 to 10 is 11 digits, compared to when going from 0 to 9, is 10 digits.

It terms of the graph,

Essentially means every second digit (every other digit), except the last digit, is doubled and not each digit.

Overall, I think better wording in the graph would have provided less confusion.

Regardless, good job in the end on finding the bug.

1 Like

Thanks, appreciate your help!
So in the end it was neither a misunderstanding of the Luhn algorithm nor the index starting from 0, it was just sloppiness. I studied the graph, memorized the wrong number (10 insted of 9) and never checked that again.
And thanks for extending my english skills: I didn’t know that ‘every other’ actually means every second.

1 Like

No worries! We are all here to learn regardless.

Glad that i was able to help!

1 Like