# Credit Card Checker: feedback appreciated

Hi! I completed the Credit Card Checker Project and wanted to share my solution.
I’d be thankful for feedback, in case there are some mistakes or if you have any tips how to make it better!

Credit Card Checker Workspace

``````// 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 = array => {
let digitCheck = 0; //set up for card numbers that are < 16 char
let sum = 0;
let newArr = [];

for (let i = array.length - 1; i >= 0; i--) {
digitCheck++;  //increments at the beginning of each loop, used instead of i % 2 to solve the < 16 numbers problem
// this is where luhn algoritm takes place
if (digitCheck % 2 === 0) {
if ((array[i] * 2) > 9) {
newArr.push((array[i] * 2) - 9);
} else {
newArr.push(array[i] * 2);
};
} else {
newArr.push(array[i]);
}
}
// summing up all numbers in the array
for (j = 0; j < newArr.length; j++) {
sum += newArr[j];
}
// checking if modulo is 0
if (sum % 10 === 0) {
return true;
} else {
return false;
}
}

// function to find and organize valid and invalid numbers into arrays
const findInvalidCards = nestedArray => {
const invalidArr= [];
for (let k = 0; k < nestedArray.length; k++) {
validateCred(nestedArray[k]);
if (validateCred(nestedArray[k]) === false) {
invalidArr.push(nestedArray[k]);
}
}
return invalidArr;
}

// prints names of card companies that released invalid cards
const idInvalidCardCompanies = nestedArrayInvalidNum => {
const cardCompany = [];
for (let l = 0; l < nestedArrayInvalidNum.length; l++) {
if (nestedArrayInvalidNum[l][0] === 3) {
if (cardCompany.indexOf('Amex') === -1) {
cardCompany.push('Amex');
} else {
continue;
}
} else if (nestedArrayInvalidNum[l][0] === 4) {
if (cardCompany.indexOf('Visa') === -1) {
cardCompany.push('Visa');
} else {
continue;
}
} else if (nestedArrayInvalidNum[l][0] === 5) {
if (cardCompany.indexOf('Mastercard') === -1) {
cardCompany.push('Mastercard');
} else {
continue;
}
} else if (nestedArrayInvalidNum[l][0] === 6) {
if (cardCompany.indexOf('Discover') === -1) {
cardCompany.push('Discover');
} else {
continue;
}
} else {
} else {
continue;
}
}
} return cardCompany;
}

console.log(idInvalidCardCompanies(batch));
``````
8 Likes

I’ve been banging my head over this challenge, struggling to even start so trust me when I say i’m so impressed

7 Likes

You can simplify the sum calculator code:

``````sum = newArr.reduce((acc, val) => acc + val);

``````
2 Likes

Saved me with the digitCheck++ as I couldn’t get it to work using i % 2

1 Like

Hello I’m 32% through the Front End Developer path.
This is my solution for the Credit Card Check - creditCardNumbers/main.js at main · fquinn454/creditCardNumbers · GitHub
Thanks

Hello everyone, this one was a challenge for me… here’s my solution with some variables… you can check it up, maybe there’s a cleaner way to do it, i tried my best… open to hear some feedbacks.

``````// 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 plus some extra
const batch = [ valid1, valid2, valid3,valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5, 65454654654654, '1941843216546', 31498432134651];

// typecasting our string into an integer
let toNumber = num => Number(num);

const validateCred = (arr) => {
//convert to Array in case the input is a string or number
let array= arr;
if(typeof array === 'string'|| typeof array === 'number'){
array = Array.from(String(arr), toNumber);
}
let indexCheck = 0; //set up for card numbers that are < 16 char
let sum = 0;
let newArr = [];

for (let i = array.length - 1; i >= 0; i--) {
indexCheck++;  //increments at the beginning of each loop to solve the < 16 numbers problem

// this is where luhn algoritm takes place
if (indexCheck % 2 === 0) {
let num = array[i] * 2;
if (num > 9) {
newArr.push(num - 9);
} else {
newArr.push(num);
};
} else {
newArr.push(array[i]);
}
}
//Here is where we add the values to sum
for(let i=0;i<newArr.length;i++){
sum+= newArr[i];
}

//Now we validate the card
if (sum % 10 === 0) {
return true;
} else {
return false;
}
};
//console.log(validateCred(mystery2));

const findInvalidCards=(array)=>{
let invalid = validateCred(array);
if (invalid === false){
return true;
}else{
return false;
}
}
//console.log(findInvalidCards(valid1));

const idInvalidCardCompanies =(array)=>{
let companies=[];
let company;
for(let i=0; i<array.length;i++){
//convert to Array in case the input is a string or number
let arr= array[i];
if(typeof arr === 'string'|| typeof arr === 'number'){
//function toNumber already created in the lines above
arr = Array.from(String(arr), toNumber);
}
switch (arr[0]){
case 3: company= `Amex (American Express)`;
break;
case 4: company= `Visa`;
break;
case 5: company= `Mastercard`;
break;
case 6: company= `Discovery`;
break;
}
let index= findInvalidCards(array[i]);
if(index===true){
if(companies.includes(company)){
continue;
}else{
companies.push(company);
}
}
}
return companies;
}
//console.log(idInvalidCardCompanies(batch));

// This is in case we want to valid an entire Batch of cards

//check for the companies for all the cards
const idInvalidCardCompany =(array)=>{
let company;
//convert to Array in case the input is a string or number
let arr= array;
if(typeof arr === 'string'|| typeof arr === 'number'){
//function toNumber already created in the lines above
arr = Array.from(String(arr), toNumber);
}
switch (arr[0]){
case 3: company= `Amex (American Express)`;
break;
case 4: company= `Visa`;
break;
case 5: company= `Mastercard`;
break;
case 6: company= `Discovery`;
break;
}
return company;
}
//create an object with the card number and the company
const invalidCards = (cardNumber, company) =>{
return {
cardNumber,
company,
};
}

const validateBatch = (array) => {
let invalid=[];
for(let j=0;j<array.length;j++){
let indexCheck = 0;
let sum = 0;
let newArr = [];
let valid;
//convert to Array in case the input is a string or number
let arr= array[j];
if(typeof arr === 'string'|| typeof arr === 'number'){
//function toNumber already created in the lines above
arr = Array.from(String(arr), toNumber);
}
for (let i = arr.length - 1; i >= 0; i--) {
indexCheck++;
// this is where luhn algoritm takes place
if (indexCheck % 2 === 0) {
let num = arr[i] * 2;
if (num > 9) {
newArr.push(num - 9);
} else {
newArr.push(num);
};
} else {
newArr.push(arr[i]);
}
}

//Here is where we add the values to sum
for(let i=0;i<newArr.length;i++){
sum+= newArr[i];
}

//Now we validate all cards and return only the not valid
if (sum % 10 === 0) {
valid = true;
} else {
valid=false;
let cardNumber= `\${arr.join('')}`;
let company= idInvalidCardCompany(arr);
//use the fabric function to create only not valid cards with their respective companies and push it into a new array
invalid.push(invalidCards(cardNumber,company));
}
}

return(invalid);
}

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

``````

take care!

Hello Haanna,

Thank you so much for sharing your solution here and congratulations on finishing this challenge! You did amazing!

I just have a minor suggestion:

You could replace

``````if (sum % 10 === 0) {
return true;
} else {
return false;
}
``````

with

``````return sum % 10 === 0;
``````

Other than that, I think it looks pretty good!

Happy coding!

Hi Everyone, I am struggling with this one. I am on step 4 where you need to create a new function findInvalidCards().

As soon as the for statement gets to valid3, which is invalid, it looks like it gets stuck and just saves all following arrays into my invalidCred array.

Any help would be greatly 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]; // Add your functions below: let newArr = []; //empty array to hold the new values after using the Luhn algorithm. const validateCred = arr =>{ //for loop to iterate through each element in the array. Iterating from right to left. for (let j = arr.length-1; j >= 0; j--){ //if statements to filter out every other element. All element with an even index need to be multiplied by 2. if ( j % 2 === 0){ let num = arr[j] * 2; //if statement checks to see if the new value is two digits. If it is, we will minus 9 and add this new value to the newArr using unshift method. if (num > 9){ newArr.unshift(num-9); } //If the num after is only 1 digit, it is added to the newArr as it is. else{ newArr.unshift(num); }; } //If the elements index is odd, we can add the element to the newArr without making any changes to the value. else{ newArr.unshift(arr[j]); }; }; //creating a variable to hold the sum of all of the elements in the new array. I used the reduce method to achieve this. let sum = newArr.reduce((p,c)=>{ return p + c; }, 0); //Checks if the sum of the array is divisible by 10 with a remainder of 0. If it is, the function will return true (credit card number is valid). if(sum % 10 === 0){ return true; } //If sum is not divisible by 10, the function will return false (credit card number is invalid). else{ return false; }; }; //This function will take an array of nested arrays as a parameter. It will iterate through each array and perform the funciton validateCred. const findInvalidCards = nestedArr=>{ //new variable declared to hold the invalid arrays. let invalidCred =[]; // for statement will iterate through the nested arrays from left to right. for(let i = 0; i < nestedArr.length; i++){ // if the return value of validateCred(arr) is false, the array will be added to our new array.the .push method is used to perform this. if(validateCred(nestedArr[i]) === false){ invalidCred.push(nestedArr[i]); } }; console.log(invalidCred); }; console.log(findInvalidCards(batch));

I think your `validateCred` function works correctly only for cards that have an even-numbered length. Note that if `j` is the index, then `j % 2 === 0` would be `true` when the index is an even number, which doesn’t match the algorithm for cards that have an odd length.
Your `findInvalidCards` seems to work right, but you might want to `return` the result instead of doing `console.log( )`
You’d expect to see invalid cards after `valid3` because `batch` consists of 5 valid cards, then 5 invalid cards, then 5 “mystery” cards (meaning cards that may be valid or invalid).