# Credit Card Checker Challenge Project (JavaScript)

Nice use of the spread operator! Definitely took some inspiration for my own code

I was thinking you could change `let isEven = false` to just `let isEven`, making it a truthy value. That way you could change to `!isEven` in your if-statement, which would make it more readable.

And in your `convertToValid()` function you could add a step to remove the check digit from the original array; then calculate the correct check digit; then append.

I’ll post my solution below if you’re interested. Thanks for sharing!

Hey @ndrklmr,

Thanks for taking a look at my code. I am glad you found some inspiration.

I would love to see your version. Especially if you have coded what you recommended for my convertToValid().

Looking forward to it!

Put way to much effort in this one; but learned a ton!

1 Like

There you go!

1 Like

Here’s my code. Might not be the most efficient, but it works.

// All valid credit card numbers const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]; const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9]; const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6]; const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5]; const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6]; // All invalid credit card numbers const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5]; const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3]; const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4]; const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5]; const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4]; // Can be either valid or invalid const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]; const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9]; const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3]; const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3]; const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3]; // An array of all the arrays above const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5]; // Add your functions below: function validateCred(arr) { let sum = 0; let arrEven; if (arr.length % 2 === 0) { arrEven = true; } else { arrEven = false; }; for (i = arr.length - 1; i >= 0; i--) { if (arrEven) { if (i % 2 !== 0) { sum += arr[i] } else if (arr[i]*2 > 9) { sum += arr[i]*2 - 9; } else { sum += arr[i]*2 }; } else { if (i % 2 == 0) { sum += arr[i] } else if (arr[i]*2 > 9) { sum += arr[i]*2 - 9; } else { sum += arr[i]*2 }; }; }; return sum % 10 === 0; }; function findInvalidCards(arr) { const invalidCards = arr.filter(item => validateCred(item) === false) return invalidCards; }; function idInvalidCardCompanies(arr) { let companies = [] const badCards = findInvalidCards(arr) for (let card of badCards) { if (card[0] === 3 && companies.indexOf('Amex') === -1) { companies.push('Amex') } else if (card[0] === 4 && companies.indexOf('Visa') === -1) { companies.push('Visa') } else if (card[0] === 5 && companies.indexOf('Mastercard') === -1) { companies.push('Mastercard') } else if (card[0] === 6 && companies.indexOf('Discover') === -1) { companies.push('Discover') } else if (card[0] < 3 || card[0] > 6) { console.log(`Company not found for card \${card.join('')}`) }; } return companies; }; console.log(idInvalidCardCompanies(batch))

I included Project Extension 7.2 and 7.3 for anyone interested btw

Here is my solution for the Credit Card Checker Challenge, with comments to easier understanding
Credit Card Checker

1 Like

Here is my solution for this challenge. I honestly don’t know if it’s clean or if it’s a mess. It works, that’s important. But when it comes to cleanliness I honestly don’t know.

The code => Credit Card Checker GitHub

hi @tomek11 I just checked your code and valid1 and valid2 are returning false. I think that the problem is that your are checking for even indices and not the actual place of the item, like when we use index starting and 1. I mean, item in place 1 stay the same, item in place 2 is *2, …
You could add a +1 here like this

``````if (i + 1 % 2 !== 0)
``````

That way you are evaluating item at their real placement.
I handled this wit index++, Im using map, so every iteration is a new element and index, the index dont stay +1 for the next iteration

``````copyCard = copyCard.map((element, index) => index++ % 2 ? element*2 > 9 ? element*2 - 9 : element*2 : element);
``````

Hi @ricard0g I just checked your code and I found 2 problems

1. You are iterating from left to right and not from right to left as the algorithm indicate.
This will return the wrong numbers to sum (more about that at the second point). For example the valid1 should return
[ 8, 5, 6, 9, 3, 7, 5, 9, 0, 8, 0, 1, 3, 8, 0, 8 ]
[8, 5, 6, 2, 5, 7, 7, 7, 5, 1, 0, 9, 2, 7, 9, 5]
This also makes valid3 return false and invalid3 return true because those arrays have 15 items and not 16 like the other ones.
2. the second problem is that you are checking if the index is even and not the real placement of the item in the array. For example an item at index 0 is the first item, a item at index 1 is the second item… so you should check item at index 1 even if the index is not even because its position it is even.

Thanks A LOT! I really appreciate the time you took to check my code. People like you make better developers.

So, I checked both your points and you’re completely right. My algorithm was wrong. The thing is that your first point was completely accurate but I somehow achieved the wanted behavior of the algorithm without really applying what you said on your second point.

Here’s the code → Credit Card Checker Corrected GitHub

Please correct me if I am wrong again, but this time I think it’s fine!

1 Like

Hi, I checked your code and it’s working for the invalid cards but all the valid ones are returning false. I think that point 2 is still the main issue for that behavior

Hii, this my repo link for the project:
(credit-card-checker/main.js at main · iamsharkawy/credit-card-checker · GitHub)

1 Like

hi, I just checked your code and looks good but be careful with reverse. It will mutate the array in place, this means that the original card array will be reversed too. This could bug your code.

1 Like

Hi, this is my link for the project. Had some challenges along the way so hoping for some feedback.

This is my Credit card checker project. I had some challenges, so hoping for some feedback. Thank you

Hi! Posted my solution earlier already and put in way to much effort, so I decided to organize the code better to make it easier to navigate. Note that I included Task 7.2 and 7.3 for anyone interested!

Challenge Project: Cre… | JavaScript Workspace | Codecademy

ps. Unfortunately I can’t remove the gist from the OP anymore, so yeah… just use the workspace ^^

Link to a project on my GitHub

Would love to get any review on my solution. I completed all the major and additional tasks. Took some time to debug it, but finally, everything works properly.

Have a great coding day everyone!