# Credit Card Checker Challenge Project (JavaScript)

This is most likely correct but maybe try logging an array after applying the function and just check. So in your original code it would be:

``````console.log(mystery1);
// output is the origional array
console.log(validateCred(mystery1));
// output is false
// here is the check
console.log(mystery1);
``````

I think that one is a different length than the other arrays.
Since this array has an odd length, you would need to double (and then possibly subtract 9) from the numbers at the at the odd indices instead of the even ones.

Notice that in what you had, you didn’t change the next-to-last number 3.

Oh thanks I was doing every other from right. That’s it!

This is my final code for this project. After looking at the solution I realize that some of my code could have been simplified haha. Any other feedback would be much 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]

// 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 validateCred = arr => {
let count = 1;
let digits = [];
for (let i = arr.length - 1; i >= 0; i--) {
if (count % 2 === 0) {
arr[i] = arr[i] * 2;
if (arr[i] > 9) {
arr[i] = arr[i] - 9;
}
digits.push(arr[i]);
}
digits.push(arr[i]);
count++;
}
const sum = digits.reduce((a, b) => a + b, 0);
if (sum % 10 === 0) {
return true;
} else {
return false;
}
}

const findInvalidCards = arr => {
let invalidCards = [];
for (let i = 0; i < arr.length; i++) {
let card = arr[i];
if (validateCred(card) === false) {
invalidCards.push(card);
}
}
return invalidCards;
}

const idInvalidCardCompanies = arr => {
let companies = [];
for (let i = 0; i < arr.length; i++) {
let number = arr[i];
if (number[0] === 3) {
if (!companies.includes('Amex')) {
companies.push('Amex');
}
} else if (number[0] === 4) {
if (!companies.includes('Visa')) {
companies.push('Visa');
}
} else if (number[0] === 5) {
if (!companies.includes('Mastercard')) {
companies.push('Mastercard');
}
} else if (number[0] === 6) {
if (!companies.includes('Discover')) {
companies.push('Discover');
}
} else {
}
}
return companies;
}
``````
1 Like

Hello, here is my project, I used mostly loops and switch statements. It took me about 3 hours to complete.

I’m not sure if this is the right way to share as I’m new at this. But here goes:

/ 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 validateCred = (arr) =>{
reverseArr = ;
//reverse array to iterate from “right to left”
for(let i = arr.length - 1; i >= 0; i–){
reverseArr.push(arr[i]);
}
//every 2nd item * 2 // > 9 = item - 9
for(let j = 0; j < reverseArr.length; j++){
if(j % 2 !== 0){
reverseArr[j] = reverseArr[j] * 2;
if(reverseArr[j] > 9){
reverseArr[j] = reverseArr[j] - 9;
}
}
}
let cardConfirm = reverseArr.reduce((previousValue, currentValue) =>previousValue + currentValue)
if(cardConfirm % 10 === 0){
return true;
}else{
return false;
}
//return reverseArr;
};

//console.log(validateCred(valid3));

const findInvalidCards = (batchArr) =>{
let invalidCards =
for(let item = 0; item < batchArr.length; item++){
//console.log(item);
//console.log(batchArr[item]);
if(validateCred(batchArr[item])){
continue
}else{
invalidCards.push(batchArr[item]);
};
}
return invalidCards;
};

//console.log(findInvalidCards(batch));

const idInvalidCardCompanies = (arr) =>{
let invalidcards = findInvalidCards(arr);
let invalidCompany = ;
let filteredCompany = ;
let company;
for(let i = 0; i < invalidcards.length; i++){
switch(invalidcards[i][0]) {
case 3:
company = ‘Amex (American Express)’;
if(invalidCompany.indexOf(company) === -1) {
invalidCompany.push(company);
};
break;
case 4:
company = ‘Visa’;
if(invalidCompany.indexOf(company) === -1) {
invalidCompany.push(company);
};
break;
case 5:
company = ‘Mastercard’;
if(invalidCompany.indexOf(company) === -1) {
invalidCompany.push(company);
};
break;
case 6:
company = ‘Discover’
if(invalidCompany.indexOf(company) === -1) {
invalidCompany.push(company);
};
break
default:
company = ‘Unknown Company’
if(invalidCompany.indexOf(company) === -1) {
invalidCompany.push(company);
};
}
}
return(invalidCompany);
}

console.log(idInvalidCardCompanies(batch));

I went the long way for the validation part for some reason.

Create main.js · mrygrcbsc/Credit-Card-Checker@8b4df4e · GitHub try to check mine.

// 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 = arr => { const arrClone = arr.slice([0]); const arrRev = arrClone.reverse(); //console.log(arrRev); const arrFirst = arrRev.shift(); //console.log(arrRev); const arrCheck = []; const arrFix = []; const arrFin = []; let num = 0; for (let i = 0; i < arrRev.length; i+=2){ arrCheck.push(arrRev[i]*2) } //console.log(arrCheck) for (let i = 1; i < arrRev.length; i+=2){ arrCheck.push(arrRev[i]) } //console.log(arrCheck); for (let i = 0; i < arrCheck.length; i++){ if (arrCheck[i] > 9) { arrFix.push(arrCheck[i]-9); } else { arrFix.push(arrCheck[i]); } } arrFix.unshift(arrFirst); //console.log(arrFix); for (let i = 0; i < arrFix.length; i++){ num += arrFix[i] } return num } //console.log(validateCred(valid1)) //console.log(test) invalidCards = [] const findInvalidCards = arr => { for (let i = 0; i < arr.length; i++){ const validCheck = validateCred(arr[i]); //console.log(validCheck); if (validCheck % 10 === 0){ invalidCards.push(arr[i]) } } return invalidCards; } findInvalidCards(batch); const idInvalidCardCompanies = arr =>{ companyIdFound = []; companyMail = []; for (let i = 0; i < arr.length; i++){ const idCheck = arr[i][0]; //console.log(idCheck); if (!companyIdFound.includes(idCheck)) { companyIdFound.push(idCheck) } } //console.log(companyIdFound); for (let i = 0; i < companyIdFound.length; i++){ switch (companyIdFound[i]){ case 3: companyMail.push('Amex'); break; case 4: companyMail.push('Visa'); break; case 5: companyMail.push('Mastercard'); break; case 6: companyMail.push('Discover'); break; default: companyMail.push('Company not found') } } } idInvalidCardCompanies(invalidCards); console.log(companyMail);

Let me know what you think of my solution and how I can improve on it!

// All valid credit card numbers 55 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: //should return true when valid, false if invalid //does not mutate orig array const validateCred = arr => { let counterTimesTwo = 0; let counter = 0 + arr[arr.length-1]; let totalSum = 0; for(let i = arr.length -1; i >= 0; i-=2) { if(arr[i-1] * 2 >= 10){ counterTimesTwo += arr[i-1] * 2 - 9; } else if (arr[i-1] >= 0){ counterTimesTwo += arr[i-1] * 2; }; if(arr[i-2] >= 0) { counter += arr[i-2]; }; }; totalSum = counter + counterTimesTwo; if(totalSum%10 === 0) { return true; } else { return false; } }; const findInvalidCards = arrNest => { let invalidArr = []; for(let i = 0; i < arrNest.length; i++) { if(validateCred(arrNest[i]) === false) { invalidArr.push(arrNest[i]); }; }; return invalidArr; }; const idInvalidCardCompanies = arrNest => { let processArr = []; let exportArr = []; for(let i = 0; i < arrNest.length; i++) { if(arrNest[i][0] === 3) { processArr.push('Amex (American Express)'); } else if(arrNest[i][0] === 4) { processArr.push('Visa'); } else if(arrNest[i][0] === 5) { processArr.push('MasterCard'); } else if(arrNest[i][0] === 6) { processArr.push('Discover'); } else { processArr.push('Company Not Found'); }; }; if (processArr.includes('Amex (American Express)')) { exportArr.push('Amex (American Express)'); } if (processArr.includes('Visa')) { exportArr.push('Visa'); } if (processArr.includes('MasterCard')) { exportArr.push('MasterCard'); } if (processArr.includes('Discover')) { exportArr.push('Discover'); } if (processArr.includes('Company Not Found')) { exportArr.push('Company Not Found'); }; return exportArr; }; console.log(findInvalidCards(batch)); let testCards = findInvalidCards(batch); console.log(idInvalidCardCompanies(testCards)); //console.log(validateCred(invalid1));

Fun Challenge!

Here is my repo.

Nice job. I seems like it does the job. Two things I found really helpful were to use Set instead of Array to create unique lists. The second is using the modulo operator ( ie. x % 2) to create actions that happen every second time.

You are definitely mutating the array at every doubling point. However, your code didn’t working. The cause is the %9. 9*2%9 = 0, when the luhn algorithm would expect it to be 9. Here is the test of the following code:

``````console.log(`Before running: \${valid3}`);
console.log(`Should return true: \${validateCred(valid3)}`);
console.log(`After running: \${valid3}`);
//____________
//Console Log Results
//Before running: 3,7,1,6,1,2,0,1,9,9,8,5,2,3,6
//Should return true: false
//After running: 3,5,1,3,1,4,0,2,9,0,8,1,2,6,6
``````

Yo

My code

This is my Credit card checker .
For me it was very tricky, above all at the beginning so maybe i can write the code in a better way let me know if you have any tips and what do you think about it!!

If you’re reading this, don’t quit this challenge!

You will achieve success! I too wanted to quit because it was the hardest challenge so far. After two days working on this challenge, I’m proud to say I’ve completed this challenge!

Please feel free to review my code for your review. There are many paths to success. Shift your mindset of reaching a hurdle into a learning opportunity.

// 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: // step 3 const validateCred = (arr)=>{ // Print original array. console.log("Original array: " + arr); // How many elements in the array? let length = arr.length console.log(`Array has \${length} elements`) // Copy array to NOT mutate values of original array. let newArray = arr.slice(); console.log("Unmutated new array: " + newArray); // Reverse the new array. let reverseArray = newArray.reverse(); console.log("Reverse array: " + reverseArray); // Multiply odd digits by 2. let doubleArray = []; const multiply = (reverseArray)=> { for (let i = 0; i < reverseArray.length; i++) { if (i % 2 === 0) { doubleArray.push(reverseArray[i]); } else { doubleArray.push(reverseArray[i] * 2) } } } multiply(reverseArray); console.log("Double array: " + doubleArray); // Subtract 9 from number if number is over 9. let finalArray = []; const subtract = (doubleArray)=> { for (let j = 0; j < doubleArray.length; j++) { if (doubleArray[j] > 9) { doubleArray[j] = doubleArray[j] - 9; finalArray.push(doubleArray[j]); } else { finalArray.push(doubleArray[j]); } } } subtract(doubleArray); console.log("Final array: " + finalArray); // Add up all the numbers in the array. const sum = finalArray.reduce((previousValue, currentValue) => previousValue + currentValue, 0); console.log("Sum from array: " + sum); // Test if sum modulo 10 is 0. if(sum % 10 === 0) { console.log("Credit card is valid."); return true } else { console.log("Credit card is invalid.") return false } }; // Call validateCred() with any valid or invalid array as an argument. validateCred(valid1); // step 4 let invalidCards = []; const findInvalidCard = (arr)=>{ for(let k = 0; k < arr.length; k++) { validateCred(arr[k]); if(validateCred(arr[k]) === false) { invalidCards.push(arr[k]) } } console.log(invalidCards) let numOfInvalidCards = invalidCards.length console.log("Number of Invalid Cards: " + numOfInvalidCards); }; findInvalidCard(batch); // step 5 // Push list of companies into an array. let companyList = []; // Identify the credit card companies that have possibly issued these faulty numbers. const idInvalidCardCompanies = (arr)=>{ for (let l = 0; l < arr.length; l++) { switch(arr[l][0]) { case 3: console.log("Amex"); companyList.push("Amex") break; case 4: console.log("Visa"); companyList.push("Visa") break; case 5: console.log("Mastercard"); companyList.push("Mastercard") break; case 6: console.log("Discover"); companyList.push("Discover") break; default: console.log("Company not found"); break; } } // Print array of companies console.log(companyList); let uniqueList = []; const oneCompanyName = (companyList)=>{ for(let m = 0; m < companyList.length; m++) { if(uniqueList.indexOf(companyList[m]) === -1){ uniqueList.push(companyList[m]) } } return uniqueList; } // Print list of unique companies--no duplicates. console.log(oneCompanyName(companyList)); }; idInvalidCardCompanies(invalidCards);