Credit card details are frequently used online and you may be wondering how a website checks if a card number is legitimate or not. During my CodeAcademy “Back-End Engineer” I have tackled the Credit Card Checker project which involves using the Luhn algorithm to check credit card numbers.
Quoting Wikipedia (https://en.wikipedia.org/wiki/Luhn_algorithm#Description):
“Each of the numbers 79927398710, 79927398711, 79927398712, 79927398713, 79927398714, 79927398715, 79927398716, 79927398717, 79927398718, 79927398719 can be validated as follows.
Double every second digit, from the rightmost: (1×2) = 2, (8×2) = 16, (3×2) = 6, (2×2) = 4, (9×2) = 18
Sum all the individual digits (digits in parentheses are the products from Step 1): x (the check digit) + (2) + 7 + (1+6) + 9 + (6) + 7 + (4) + 9 + (1+8) + 7 = x + 67.
If the sum is a multiple of 10, the account number is possibly valid. Note that 3 is the only valid digit that produces a sum (70) that is a multiple of 10.
Thus these account numbers are all invalid except possibly 79927398713 which has the correct check digit.”
My solution is in JavaScript and CodeAcademy provide several batches of valid and invalid credit card numbers to check the solution against. I’ve not included those arrays of numbers in the below as CodeAcademy generated them.
This exercise was to help build on my knowledge of loops and arrays in JavaScript.
// validateCred function to check card number is valid
// https://en.wikipedia.org/wiki/Luhn_algorithm#Description
function validateCred(array){
var arrayLength = array.length;
var modifiedArrayLength = arrayLength-2;
var total = 0;
let j = arrayLength-1;
let i = modifiedArrayLength;
/*
do loop to multiple the 2nd to last number, minus 10 if the multiplication is in double digits and then do the same to every other number
*/
do {
var x = (array[i]*2);
if (x >= 10){
x = (x-9)
}
i = i-2;
total = total+x;
} while (i >=0);
/*
do loop to add the last number and every other number
*/
do {
var y = (array[j]);
j = j-2;
total = total+y;
} while (j >=0);
/*
checksum as the total should have no remainder when divided by 10 if the card is valid
*/
checkSum = total%10;
if (checkSum===0){
// card is valid
return true
}
// card is not valid
return false
}
// end of validateCred function
// findInvalidCards function to search a nested array for invalid cards
function findInvalidCards(nestedArray){
invalidCards = [];
var lengthOfArray = nestedArray.length;
var i = lengthOfArray-1;
do {
if(validateCred(nestedArray[i])===false){
invalidCards.push(nestedArray[i]);
}
i--;
} while (i >= 0);
return invalidCards
}
// end of findInvalidCards function
// idInvalidCardCompanies function to identify which credit card company
function idInvalidCardCompanies(anotherNestedArray){
var invalidCards = findInvalidCards(anotherNestedArray);
var lengthOfCards = invalidCards.length;
var i = lengthOfCards-1;
var arrayOfCompaniesSorted = [];
// arrays to hold each companies invalid cards (if any)
// amex starts 3
var amex = [];
// visa starts 4
var visa = [];
// mastercard starts 5
var mastercard = [];
// discover starts 6
var discover = [];
do {
switch(invalidCards[i][0]){
case(3):
amex.push(invalidCards[i])
break;
case(4):
visa.push(invalidCards[i])
break;
case(5):
mastercard.push(invalidCards[i])
break;
case(6):
discover.push(invalidCards[i])
break;
}
i--;
} while (i >= 0);
// return the card companies in one nested array
arrayOfCompaniesSorted.push(amex);
arrayOfCompaniesSorted.push(visa);
arrayOfCompaniesSorted.push(mastercard);
arrayOfCompaniesSorted.push(discover);
return arrayOfCompaniesSorted
}
// end of InvalidCardCompanies function