Credit Card Checker Challenge Project (JavaScript)

Your feedback really motivated me, because I’m still trying to believe that I can become a developer, thanks!

Hi All!
I’ve finished this task and, as I complete the challenge writing the fix function (task 7), I’m sharing it!
It seems to working out. If you have time, please test it and give me a feedback. All comments are welcome!
the link: Thanks :slight_smile:

I’ve looked through a few and haven’t seen any that look like mine. I’m most curious to know how others dealt with the varying card length problem. I first check to see if the card length is even or odd and proceed accordingly. Any tips on how to make this more efficient are greatly appreciated!!

Enter just digits and click Validate. Works for any number of digits.

https://www.codecademy.com/workspaces/610cb623e53ca95b564ecbe4

Hello,

This is my solution for the project:

Here is my solution :slight_smile:

https://gist.github.com/eedd96b519e6f56fa98f9e15bc9de88d

This project took me longer than it should have, but here is my gist!

Hello. I recently completed my credit card checker project. You can find it here: dow-mp: Credit Card Checker

I’d love any feedback on my solution. Thanks in advance.

I just finished this project, and all my code (along with the provided credit card arrays) are included on my git repository provided below:

I’ve included some brief summary/key points of my workflow there if you are curious.

Let me know what you think, thank you!

I’m just now realizing that my code doesn’t work for even numbered cards. What would y’all suggest would be the workaround for this?

Your validateCred function calculates your checkSum from left to right instead of right to left. This will fail whenever you have an even number of digits in the card.

Consider a small card number with 6 digits, you should be doubling the 1st, 3rd, and 5th digits. Your method will double the 2nd, 4th, and 6th.

Line 31 in your solution is:
for(let i = 0; i < creditArr.length; i++) { // Double every other number

This should be:
for (let i = arr.length - 1; i >= 0; i–) { // Calculate checksum for card from right to left

You’ll also need to change the rest of your logic to account for the fact that you’re counting right to left but that particular line encapsulates the issue.

For reference, this is what I came up with which is pretty close to the solution provided:

function validateCred(arr) {
  let checkSum = 0;

  for (let i = arr.length - 1; i >= 0; i--) {
    // Calculate checksum for card from right to left
    let currValue = arr[i]; // Get value of current card digit
    if ((arr.length - i) % 2 === 0) { 
      // Double every other number, starting with the second digit from the right
      currValue *= 2;
      if (currValue > 9) {
        currValue -= 9; // If doubled value > 9, reduce by 9
      }
    }
    checkSum += currValue; // Update checksum
  }

  if (checkSum % 10 === 0) {
    return true;
  } else {
    return false;
  }
}
1 Like

My solution - Credit Card Validator

Seems pretty close to the provided solution but has enough differences that I thought I’d share.

Your comments about length were an issue I struggled with too. Did using the length work for you?

 if ((arr.length - i) % 2 === 0) {

Maybe I was overthinking this, but thought that the length of the string would throw the code off. E.g. If the length was 16 characters, then it made sense to me that the currValue%2 should equal 0 (i.e. the 2nd last character, or 15th from the beginning is at index 14). But if it was 15 characters, you’d actually want currValue%2 = 1 (in order to start at index 13) wouldn’t you?

I ended up using an extra counter in my code for this chunk after spending a long time trying to figure out what I did wrong (initially taking the modulo of the digit itself in a moment of silliness, and taking about an hour to realize I did that!).

const validateCred = (arr) => {

let dig; //create variable to store digits while iterating through the card number

let j = 1; //create variable to keep track of multiplier

let sumDig = 0; //initialize sum of digits to 0

//Iterate through digits starting at the end [length - 1 since arrays start at 0], multiplying every 2nd digit by 2, and subtrating 9 from any two digit numbers 

  for (let i = arr.length - 1; i>=0; i--){ 
    if (j % 2 === 0){
      dig = arr[i] * 2;
    } else {
      dig = arr[i];
    }
   
 if (dig > 9){
      dig -= 9;
    }

    sumDig += dig;

    j++ //increment j to keep track of the digit from the end of the number

  }
  //determine if number is valid based on rule that it must be divisible by 10
  if(sumDig % 10 === 0){
    return 'valid'
  } else {
    return 'invalid'
  }
}

If you use only the index % 2 to decide whether you double the number at that index,
it’ll have to work differently for arrays that have an odd-number length as opposed to arrays that have an even length.

1 Like

The loop is taking advantage of a difference between the length method and the index. Length starts counting at 1 and goes up - so an array with 16 digits will return 16. Since the index starts at 0, the index of the final digit will be 15 (one less than the length).

We can take advantage of that behavior in this loop since the index (i) of the first character from the right is 15, the formula (arr.length - i) results in 16 - 15 (or 1) for the first loop. Since 1 % 2 === 1, we do not double.
When we move one spot to the left to the 2nd character (with an index of 14), we’re still evaluating (arr.length - i) but i is now 14. Now 2 % 2 === 0 so we double.
For the 3rd digit, we get 16-13 → do not double
For the 4th digit, we get 16-12 → double
etc.

Since we started from the right, it doesn’t matter how many digits there are in total. All that matters is how many digits we are from the rightmost digit. If the card had 51 digits, the first loop would just be 51 - 50 and still doesn’t double. The loop will keep switching back and forth between double and do not double until it iterates through the entire card number.

We’ve got a mask that covers every other character beneath it. Slide it to the right or left, it covers the others. alternately.

We’re doubling the ones that are not covered. The logic does not need to permeate into the loop. It governs the loop.

How?

thanks for clarifying - this is a really nice solution and a really clear explanation. I misread it the first time as 1 rather than i, so it makes a lot more sense now.

I ended up at the same point (a counter starting at 1), but needing to declare an extra variable, so nice to see a way I could have simplified my code a bit.

I’ll just clarify that in my solution, I used a 2nd counter (not the index) which I had to manually increment at the end of my loop. I declared the variable i to iterate through my loop (counting down from the end of the array), and the variable j to keep track of the iteration I was on. I initialized j to 1 rather than 0, so that I could make my condition j % 2 = 0 (i.e. every 2nd digit) in more “intuitive” counting rather than the usual index approach starting at 0.

I’d do it differently next time, but in case this helps anyone else trying to make sense of my code.