# Credit Card Checker Project

Hello,
I am doing the Credit Card Checker Project and am finding it a little difficult to complete! I am happy with step one and two, but step 3 is proving difficult.

Here is my code:

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

//Step 1
const validateCred = array => {
// Swapping the array around
const invertarray = array.reverse();

//Checking the even index numbers
for(let i = 0; i < invertarray.length; i++){
if (i % 2 !=0){
invertarray[i] = invertarray[i]*2;
if(invertarray[i] > 9){
invertarray[i] -=9;
}
}

}
const sum = invertarray.reduce((a, b) => a + b, 0); //using reduce to add the numbers
if(sum % 10 === 0){
return true;
} else {
return false;
}

}

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

// Step 2
function findInvalidCards(creditCardNumbers) {
for (const card of creditCardNumbers) {
if (validateCred(card) === false) {
console.log(`\${card} is \${validateCred(card)}`)
}
}
}

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

// Step 3

const creditCardCompanies = ['Amex(American Express)', 'Visa', 'Mastercard', 'Discover']

const idInvalidCardCompanies = invalidCompanies => {

}

/*
First Digit     Company

3               Amex(American Express)
4               Visa
5               Mastercard
6               Discover
*/

``````

Any hints and tips would be great!

Hi @luke_g_p
congratulations for finishing the first two steps. This is my approach for the third:
I iterated through each array and stored the first digit of it to a variable. Then I used a switch statement to check to which company it belongs. Hope that helps, good luck!

Hi Mirja, how did you iterate through and find the first number?

I decided to add to step 2:

``````function findInvalidCards(creditCardNumbers) {
for (const card of creditCardNumbers) {
if (validateCred(card) === false) {
const creditCardCompanies = ['0','0','0','Amex(American Express)', 'Visa', 'Mastercard', 'Discover'];
const firstCardNumber = card[0];
console.log(`\${card} is \${validateCred(card)} and belongs to \${creditCardCompanies[firstCardNumber]}`);
}
}
}

findInvalidCards(batch);
``````

Whilst it doesn’t meet the requirements of the task, it logs this to the console:

``````5,9,7,2,9,0,1,5,7,7,7,5,2,6,5,8 is false and belongs to Mastercard
3,8,6,8,3,2,2,9,3,6,9,1,5,9,7,1 is false and belongs to Amex(American Express)
4,2,9,9,5,8,4,7,0,3,9,5,5,5,3 is false and belongs to Visa
5,6,9,5,7,5,1,3,9,5,2,2,1,2,0,3 is false and belongs to Mastercard
4,1,8,6,8,7,2,5,7,9,1,0,2,7,3,1 is false and belongs to Visa
4,2,4,1,0,6,8,3,9,2,0,7,4,8,3 is false and belongs to Visa
3,0,2,3,5,3,2,3,9,0,2,0,7,5,3,2,1,0,6 is false and belongs to Amex(American Express)
3,9,0,5,1,4,9,3,1,5,7,7,9,4,9,8 is false and belongs to Amex(American Express)
``````

Any suggestions, please let me know! As it currently wouldn’t help anyone go through the list quicker in real life

with forEach() method instead of for of loop. Both ways are fine, I think.

That’s how I picked the first digit, too.

But I don’t unterstand what this is for:

With the `firstCardNumber` digit I did a switch statement and counted the companies like case 3: amex++, case 4: visa++.

I don’t understand what do you mean by that?

Thank you, I will give it a go!

I stored them in an array. Since the numbers where 3 for Amex etc, I just added three 0’s to make sure the index number matched:

The goal of this project is to create automation for this task. If we were to present my results to a clark, let’s say, it wouldn’t help them determine which ones are invalid.

Ah ok, I get what you mean now. That makes sense, of course. So instead of counting up in a switch statement, you could store the credit card number in an array. Then you could return the invalid credit card numbers and the array.length for each company. Then you’d have fulfilled the task and took into account your objection as well.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.