# Credit Card Checker Challenge Project (JavaScript)

// 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: const validateCred = array => { // Function to validate card const newArray = array.slice().reverse(); // Create new reversed array let sum = 0; // Initialise variables let ans = true; newArray.forEach((n, i) => { // Iterate each number of card if(i % 2){ // Ensure only every 2 digits used let x = n * 2; // Luhn algorithm x > 9 ? sum += (x - 9) : sum += x; } else { sum += n; }; }); sum % 10 == 0 ? ans = true : ans = false; // Truthy val determined return ans; // Return truthy } //console.log(validateCred(invalid3)) const findInvalidCards = cards => { // Function to make arr of inv cards let invalidCards = []; // Initialise new array for (const card of cards) { // Iterate each card in batch !validateCred(card) ? invalidCards.push(card) : ''; }; // If calback is false add to arr return invalidCards; // Return value }; //console.log(findInvalidCards(batch)) const idInvalidCardCompanies = invalid => { // Fnctn to make arr of inv companies let invalidCompanies = [] // Initialise arr and variables let amx = 0 let mst = 0 let vsa = 0 let dsk = 0 for (i in invalid) { // Iterate each card in batch switch (invalid[i][0]){ // Incriment each var if num present case 3: amx += 1; break; case 4: vsa += 1; break; case 5: mst += 1; break; case 6: dsk += 1; }; }; // Add company to arr if present amx > 0 ? invalidCompanies.push('Amex') : ''; vsa > 0 ? invalidCompanies.push('Visa') : ''; mst > 0 ? invalidCompanies.push('Mastercard') : ''; dsk > 0 ? invalidCompanies.push('Discovery') : ''; return invalidCompanies; // Return value }; //console.log(idInvalidCardCompanies(findInvalidCards(batch))) const makeValid = old => { // Fnctn to make card valid if not let newArray = old.slice().reverse(); // assign new arr reversed card let sum = 0; // Initialise values let ans = true; let rem = 0; newArray.forEach((n, i) => { // ***Same as first function*** if(i % 2){ let x = n * 2; x > 9 ? sum += (x - 9) : sum += x; } else { sum += n; }; }); rem = sum % 10 rem == 0 ? ans = true : ans = false; if(ans == false) { // Make up remainder with arr[0] rem <= newArray[0] ? newArray[0] -= rem : newArray[0] += (10 - rem); }; return newArray.reverse() // Return new array. } //console.log(makeValid(invalid3)) const convertToArr = str => { // Fnctn to cnvrt to arr of num arr = str.split(''); // Split to array const newArr = arr.map((a) => parseInt(a)); // map to new arr of num return newArr; // return new array } //console.log(convertToArr('123456789'))

Was the most fun so far!

Thought I’d share my code. Used many array methods and some tricks learnt throughout this unit to reduce code size. No for loop definition involved.

// 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]; const batch2 = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3]; // Add your functions below: const checkSum = arr => { // Step 1 let len = arr.length; // Double every other digit from right to left let doubled = arr.map((x, index) => (len-index)%2 === 0 ? x*2 : x); let subtracted = doubled.map(x => x>9 ? x-9 : x); // Step 3 & 4 return subtracted.reduce((accumulator, currentValue) => accumulator+currentValue)%10 ? false : true; } const findInvalidCards = nestedArr => { return nestedArr.filter(card => !checkSum(card)); } // Test findInvalidCards() //console.log(findInvalidCards(batch)); const idInvalidCardCompanies = nestedArr => { const companyDict = { 3: "Amex", 4: "Visa", 5: "Mastercard", 6: "Discover" } const invalidCompanies = []; // Find all invalid cards const inavlidCards = findInvalidCards(nestedArr); // Push company code if notalready in arr inavlidCards.forEach(card => !invalidCompanies.includes(card[0]) && invalidCompanies.push(card[0])); // Convert company codes to names const invalidCompanyNames = invalidCompanies.map(element => [3,4,5,6].includes(element) ? companyDict[element] : "Company not found"); return invalidCompanyNames; } // Test idInvalidCardCompanies() console.log(idInvalidCardCompanies(batch)); console.log(idInvalidCardCompanies(batch2));

Is it bad that I completely had no clue what to do. I literally didn’t understand anything, and I’ve been through the Javascript lessons until now. That project seemed very advanced and I’m feeling really bad about myself right now. When I saw the solution, I couldn’t even make sense of it. How am I ever expected to do this for a job?

It may help to go back through the lessons again … see how much you can do without looking at solutions or hints. Or try to extend the code in a lesson to do something else that you find useful or interesting on your own.
Repetition can be frustrating, but it does help.

The stuff on arrays (and array methods), loops, and conditionals (if statements and such) are some topics that may be useful for this project.

Hey,