This is my solution to this challenge
Really good challenge for sure. Here’s my solution:
Here guys my solution!!
Please give me feedbacks
Thanks a lot
Hello everyone!
Here is the link to my solution. Any comment will be appreciated.
// 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];
const test1 = [4, 7, 1, 6, 4, 3, 7, 8, 4, 7, 6, 6, 0, 3, 8, 9];
const test2 = [4, 5, 5, 6, 7, 7, 6, 5, 4, 5, 9, 7, 0, 5, 7, 1];
const test3 = [5, 3, 8, 7, 9, 9, 3, 6, 7, 8, 7, 0, 9, 4, 7, 6];
const test4 = [3, 7, 6, 9, 7, 2, 6, 3, 4, 2, 6, 2, 4, 5, 1];
const test5 = [6, 0, 1, 1, 0, 6, 7, 3, 3, 1, 3, 0, 0, 3, 8, 1, 7, 9, 5];
// An array of all the arrays above
const batch2 = [test1, test2, test3, test4, test5];
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 digit = 0;
let c = 0;
for (let i = arr.length - 1; i >= 0; i--) {
c++;
if (c % 2 == 0 && i != arr.length - 1) {
digit = arr[i] * 2;
if (digit > 9) {
digit -= 9;
}
sum += digit;
} else {
sum += arr[i];
}
}
if (sum % 10 == 0) {
return true;
} else {
return false;
}
}
function findInvalidCards(arr) {
let inv = [];
for (let i = 0; i < arr.length; i++) {
if (validateCred(arr[i]) == false) {
inv.push(arr[i]);
}
}
return inv;
}
function idInvalidCardCompanies(arr) {
let com = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i][0] == 3 && !com.includes("Amex")) {
com.push("Amex");
} else if (arr[i][0] == 4 && !com.includes("Visa")) {
com.push("Visa");
} else if (arr[i][0] == 5 && !com.includes("Mastercard")) {
com.push("Mastercard");
} else if (arr[i][0] == 6 && !com.includes("Discover")) {
com.push("Discover");
} else {
com.push("Company not found");
}
}
return com;
}
const invalid = findInvalidCards(batch);
console.log(idInvalidCardCompanies(invalid));
const testInvalid = findInvalidCards(batch2);
console.log(idInvalidCardCompanies(testInvalid));
Many many hours (over 12 ) later and here it is. Any feed back appreciated.
Project was frustrating, but worth it.
Here is my solution, please let me know any thoughts to improve. Thanks!
This is my solution, it might be a little bit ugly but it works and I am proud of it
My solution. I made my own isUnique function to determine if the company was already there. Realize now I could have just used .includes.
main.js
Hello everybody,
Here is my solution : CreditCardChecker
It’s my first attempt and I think it can be improved a lot.
Any comment for improvement is appreciated
I was really not feeling like iterating backwards to do the initial step of Luhn’s algorithm so I cheated and used array.reverse(). At the end I didn’t even remember switch statements existed so I created a function that iterates through an the array with duplicates and pushes only one of the instances to a new array. https://github.com/phoebewoofter/creditCardCheckerChallenger/blob/main/main.js
I started off my validateCred() with declaring a var equal to the modulo of the array length and a single .reduce() on the array. Since the length of the array would have the same modulo as the indexes that would be doubled I didn’t need to iterate backwards. However the .reduce() contained an {if, if else, else} statement that I found hard to read so I scrapped it and used:
[…arr].reverse().map().map().reduce() % === 10
This is a lot easier to read and understand and it ended up being more consice than the single .reduce(if, if else, else)
Okay, so I wanted to do the last challenge of converting an invalid credit card to a valid credit card number. It’s finally working, but initially it wasn’t. The only thing I changed was the order of my code. Instead of putting my invalidToValid function at the bottom, after screening for company names, I put it directly after my Luhn algorithm function. Why did putting it at the end return unexpected results? Can anyone help me understand this? Oh, and my invalidToValid function changes the check digit by either subtracting the remainder from the og check digit or adding (10 - remainder) to the check digit.
I know there are better and more efficient ways of writing the invalidToValid function (i.e., only having one parameter and calling the findRemainder function within the invalidToValid function). But I tried so many different things before moving the functions toward the top, I now have one of my worse versions of my code and I am thiiiiis close to rage quitting.
Here’s my solution:
Not sure if I shared this correctly but here’s my solution:
niclog/CreditCardChecker.js
Hi, I’m sending a link to my solution of the project:
I’ll appreciate your feedback.
Best regards
Pawel
Hello everyone!
I had so much fun coding this project, but it was quite challenging for me. I am always amazed by the simplicity of the solutions provided by Codecademy.
Here is the link to my project Gist: Luhn Algorithm, Credit Card Checker · GitHub
here is my solution (Codecademy export · GitHub)
This repository contains JavaScript functions for validating credit card numbers using the Luhn algorithm, identifying invalid card issuers.
Key Features
- Luhn algorithm implementation: Accurate validation of credit card numbers.
- Invalid card identification: Detects and isolates invalid card numbers from a dataset.
- Issuer identification: Determines the potential issuer of invalid cards based on the first digit.
Usage
validateCred(cardNumber)
: Validates a single credit card number using the Luhn algorithm.findInvalidCards(nestedArray)
: Identifies invalid credit card numbers from a nested array.idInvalidCardCompanies(invalidCards)
: Determines the potential issuers of invalid cards.convertInvalidToValid(invalidNumber)
: Attempts to correct invalid credit card numbers (basic implementation).