# 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 validateCard = (arrayOfCards) => { const checkDigit = arrayOfCards[arrayOfCards.length - 1]; let bit = 1; const cardDigitTotal = arrayOfCards.slice(0, arrayOfCards.length - 1).reduceRight((accumulator, value, i) => (bit ^= 1) ? (accumulator + value) : (accumulator + ((value *= 2) > 9 ? value - 9 : value)), 0); return (cardDigitTotal + checkDigit) % 10 == 0 ? true : false; }; const findInvalidCards = arrayOfCards => arrayOfCards.filter(element => !validateCard(element)); const idInvalidCardCompanies = (arrayOfCards) => { const table = { '3' : 'Amex (American Express)', '4': 'Visa', '5' : 'Mastercard', '6' : 'Discover' } return arrayOfCards.map(element => { return [table[element[0]], element.join('')]; }); }; console.time('card checker') //validateCard(valid1) //findInvalidCards(batch) idInvalidCardCompanies(findInvalidCards(batch)); console.timeEnd('card checker') console.log(idInvalidCardCompanies(findInvalidCards(batch)))

I pretty much managed to do it, but but the idInvalidCardCompanies is not working, could anybody help? Thank you

``````// 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 = [ invalid1, invalid2, invalid3, invalid4, invalid5];

let total = 0;
let validCred = (arr) => {
arr.reverse()
for(let i = 0; i < arr.length; i++){ //looping through the array
if(i%2 !== 0){ //using modulus to check every other number in the array
arr[i] * 2 //if true, muktiply number by 2
}
if(arr[i] > 9){ //if number bigger than 9, then substract 9 from it
arr[i] - 9;
}
//console.log(arr[i])
total += arr[i] //add all the total to the total var
}
//console.log(total)

arr.reverse() //reversing array to original position.
if(total % 10 === 0){
return true;
} else {
return false;
}

}

let invalidCC = [];

let findInvalidCards = (arr) => { //function to check for invalid cards, and adding them to the empty array.
arr.forEach(x => {
if(validCred(x) === false){
invalidCC.push(x);

}
})
//console.log(invalidCC)
}

findInvalidCards(batch)

let invalidCompanies = [];
let idInvalidCardCompanies = (arr) => {
for(i=0; i < arr.length; i++){
if(arr[i][0] === 3 && invalidCompanies.indexOf('Amex') === -1) {

invalidCompanies.push('Amex')

} else if(arr[i][0] === 4 && invalidCompanies.indexOf('Visa') === -1){

invalidCompanies.push('Visa')
} else if(arr[i][0] === 5 && invalidCompanies.indexOf('Mastercard') === -1){

invalidCompanies.push('Mastercard')
} else if(arr[i][0] === 6 && invalidCompanies.indexOf('Discover') === -1){

invalidCompanies.push('Discover')
} else {
}

}

console.log(invalidCompanies)
}

idInvalidCardCompanies(invalidCC)
``````

I did what you did, but in the end I changed the last else statement to an ‘else if’ and used the following:

``````    else if (arr[i][0] != 3 && arr[i][0] != 4 && arr[i][0] != 5 && arr[i][0] != 6){
}
``````

because it was console logging ‘company not found’ when it actually should know the company.

Let me know if that helps!

Ah thanks for that, didn;t realise that.

``````if(arr[i][0] === 3 && invalidCompanies.indexOf('Amex') === -1)
``````

So basically checking if card already exists, then don’t add it to the array.
But when I run it, it doesn’t return anything for some reason.

Right! not sure why that doesnt work actually! I used .includes() and set it to only add if it does not value ‘true’ already. that worked for me

else if (arr[i][0] === 6 && invalidCompanies.includes(‘discover’) != true)

There are a few issues that I have found within this code:

1. `arr.reverse()` - you are mutating the original array instead of storing a new array to mutate, which is against the constraints of the project, but I originally did this too by mistake
1. `if(i%2 !== 0)` - you are checking the index of all digits, including the check digit which can often be at an even position in the array, so the InvalidCC array often contains valid card numbers (Don’t forget that AMEX cards are 15 digits, which causes an issue with the check digit)

2. `return 'company not found'` - this will return the function and not evaluate any other element of the array as an example:

``````ENTER FUNCTION
First Iteration:
First digit of card number = 3
if company not in array
push value of AMEX to array of invalid companies
Second Iteration:
First digit of card number = 3
if company not in array
-> AMEX is already in array due to first iteration
else
EXIT FUNCTION``````
1 Like

Thankfully I managed to complete the challenge on my own and most of my solutions were fairly similar to CodeAcademy’s solutions except for the last one. I used a set to house the companies that issued invalid cards, which only allowed one occurrence of any value, then i converted the set into an array at the end.

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]; 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]; 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 batch = [ valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5, ]; function idInvalidCardCompanies(arr) { let companies = new Set(); for (let i = 0; i < arr.length; i++) { if (arr[i][0] === 3) { companies.add('Amex (American Express)'); } else if (arr[i][0] === 4) { companies.add('Visa'); } else if (arr[i][0] === 5) { companies.add('Mastercard'); } else if (arr[i][0] === 6) { companies.add('Discover'); } else { return 'Company not found'; } } let companiesArr = Array.from(companies); return companiesArr; } console.log(idInvalidCardCompanies([invalid1])); // Should print['visa'] console.log(idInvalidCardCompanies([invalid2])); // Should print ['mastercard'] console.log(idInvalidCardCompanies(batch)); // Find out which companies have mailed out invalid cards

I have completed the task. Some of my code is unorthodox I think; but I’ve completed the task successfully all the same. Please have a look at my code on github

Here’s my solution.

Used the .reduceRight method for the validateCred function.

For findInvalidCards:
const findInvalidCards = cards => cards.filter(card => validateCred(card) === false);

And a series of if statements for the idInvalidCardCompanies function.

// 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]//valid, accordint to our function 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]//valid, accordint to our function // 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 = card => { //let'sum up the numbers of the card(copy [...card] so we dont mutate it), using Luhn's algorithim: // https://content.codecademy.com/PRO/independent-practice-projects/credit-card-checker/diagrams/cc%20validator%20diagram%201.svg?_gl=1*1d18j51*_ga*NTg0OTQzNTA4Ny4xNjcyMjMzOTg4*_ga_3LRZM6TM9L*MTY3NzUxMzUxNS45LjEuMTY3NzUxMzUyNC41MS4wLjA. let sum = [...card].reduceRight((total, number, i) => { if (([...card].length - i) % 2 != 0) { return total + number } else { let modNumber = number * 2; if (modNumber > 9){ modNumber -= 9; } return total + modNumber } }) // The card is valid if the sums module of 10 is 0. return sum % 10 === 0; } //Uncoment to try function: // console.log(validateCred([4,5,3,9,6,8,9,8,8,7,7,0,5,7,9,8])); // console.log(validateCred(valid1)); const findInvalidCards = cards => cards.filter(card => validateCred(card) === false); //Uncoment to try function: // console.log(findInvalidCards(batch)); const idInvalidCardCompanies = invalidCards => { let firstDigits = invalidCards.map(card => card[0]); let invalidCardCompanies = []; if (firstDigits.findIndex(digit => digit === 3) > -1) { invalidCardCompanies.push('Amex (American Express)') } if (firstDigits.findIndex(digit => digit === 4) > -1) { invalidCardCompanies.push('Visa') } if (firstDigits.findIndex(digit => digit === 5) > -1) { invalidCardCompanies.push('MasterCard') } if (firstDigits.findIndex(digit => digit === 6) > -1) { invalidCardCompanies.push('Discover') } return invalidCardCompanies } //Uncoment to try function: // console.log(idInvalidCardCompanies([invalid1, invalid2, invalid3, invalid4, invalid5]));

That’s because your function is printing the results rather than returning them.

Hello,

I finished my code today.

This is my solution for the Credit Card Checker with the extra features requested (StringToArray() and CorrectCredit())

Alright, here’s my attempt. I think it came out pretty good and very similar to a lot of others.

So many different solutions! Very interesting going through everyone’s code and seeing how different people tackled the same project.

Here’s mine:

In your functions you have them logging the the result to the console. Try replacing the “console.log()” with “return”. That may fix your problem.

1 Like

I also noticed it and reported the bug, unfortunately now almost 3 years later @codecademy did not do anything with this feedback yet.

// 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: function validateCred(arr){ let arr1 = [] arr1.push(arr[arr.length - 1]) for (let i = arr.length - 2; i >= 0; i -= 2) { if (arr[i] * 2 > 9) arr1.push(arr[i] * 2 - 9) else arr1.push(arr[i] * 2) } for (let i = arr.length - 3; i >= 0; i -= 2) { arr1.push(arr[i]) } const sum = arr1.reduce((acc, curr) => acc + curr) if (sum % 10 === 0) return true else return false } function findInvalidCards(nArr){ const invalidCred = []; for (let i=0 ; i < nArr.length ; i++){ if (!validateCred(nArr[i])){ invalidCred.push(nArr[i]); } } return invalidCred; } function invalidCardCompanies(Arr){ let name; const companiesWithInvalidCard =[]; for(let i = 0 ; i < Arr.length ; i++){ if (Arr[i][0] === 3){ if(!companiesWithInvalidCard.includes("Amex (American Express)")){ companiesWithInvalidCard.push("Amex (American Express)"); } }else if (Arr[i][0] === 4){ if(!companiesWithInvalidCard.includes("Visa")){ companiesWithInvalidCard.push("Visa"); } }else if (Arr[i][0] === 5){ if(!companiesWithInvalidCard.includes("Mastercard")){ companiesWithInvalidCard.push("Mastercard"); } }else if (Arr[i][0] === 6){ if(!companiesWithInvalidCard.includes("Discover")){ companiesWithInvalidCard.push("Discover"); } }else { console.log("Company not found"); } } return companiesWithInvalidCard; } let invalidCardNo = findInvalidCards(batch); console.log(invalidCardNo.length); let companiesWithInvalidNo = invalidCardCompanies(invalidCardNo); console.log(companiesWithInvalidNo)

Took a while but I nailed it.

Here is the code of my project. I’ll be happy to receive any feedback! GitHub - Juneik/Credit-Card-Checker

1 Like

Hello mates!

Here’s my version of the project: Github Repo - Credit card checker.

Hoping to see any review about my code. Thanks!.

1 Like