# Credit Card Checker Challenge Project (JavaScript)

This is my solution to this challenge

Really good challenge for sure. Here’s my solution:

// Step #3 function validateCred(array) { let revArray = array.reverse(); let cardCheck = []; let cardSum = 0; for (num in revArray) { if (num % 2 == true) { let revArrayDub = revArray[num] * 2; if (revArrayDub > 9) { cardCheck.push(revArrayDub - 9); cardSum += (revArrayDub - 9); } else { cardCheck.push(revArrayDub); cardSum += revArrayDub; }; } else { cardCheck.push(revArray[num]); cardSum += revArray[num]; }; }; //console.log(`Card Check: \${cardCheck}`); //console.log(`Card sum: \${cardSum}`); if (cardSum % 10 === 0) { return 'Credit card number is valid!'; } else { return 'Credit card number is NOT valid!'; }; }; //console.log(validateCred(mystery5)); // Step #4 function findInvalidCards(array) { let invalidCards = []; let nestedBatch = batch.forEach((card) => { if (validateCred(card) === 'Credit card number is NOT valid!') { invalidCards.push(card.reverse()) }; }); return invalidCards; }; //console.log(findInvalidCards(batch)); // Step #5 function idInvalidCardCompanies(array) { let cardNums = findInvalidCards(array); let invalidCompanies = []; for (nums in cardNums) { //console.log(cardNums[nums][0]); if (cardNums[nums][0] === 3) { invalidCompanies.push('Amex'); } else if (cardNums[nums][0] === 4) { invalidCompanies.push('Visa'); } else if (cardNums[nums][0] === 5) { invalidCompanies.push('Mastercard'); } else if (cardNums[nums][0] === 6) { invalidCompanies.push('Discover'); } } let unique = (array) => { return [...new Set(array)]; } console.log(unique(invalidCompanies)) //console.log(invalidCompanies); } idInvalidCardCompanies(batch);

Here guys my solution!!
Thanks a lot

Hello everyone!
Here is the link to my solution. Any comment will be appreciated.

Credit Card Checker

``````// 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,
];

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 {
}
}
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.