chore: Strings: Credit Card Validation (#830)

* Add credit card number validator

- Validates the credit card number based on Luhn algorithm

* Test Cases: ValidateCreditCard

* Auto-update DIRECTORY.md

* Fix: Spell check

* Add references and move const inside function

* Add comments

* Fix trailing spaces

* Add short description

* Remove trailing spaces

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@im_8055
2021-11-04 13:34:52 +05:30
committed by GitHub
parent 64920bf1a3
commit 85b0571d81
3 changed files with 99 additions and 0 deletions

View File

@ -310,6 +310,7 @@
* [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.js)
* [ReverseWords](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseWords.js)
* [ScrambleStrings](https://github.com/TheAlgorithms/Javascript/blob/master/String/ScrambleStrings.js)
* [ValidateCreditCard](https://github.com/TheAlgorithms/Javascript/blob/master/String/ValidateCreditCard.js)
* [ValidateEmail](https://github.com/TheAlgorithms/Javascript/blob/master/String/ValidateEmail.js)
## Timing-Functions

View File

@ -0,0 +1,59 @@
/**
* Validate a given credit card number
*
* The core of the validation of credit card numbers is the Luhn algorithm.
*
* The validation sum should be completely divisible by 10 which is calculated as follows,
* every first digit is added directly to the validation sum.
* For every second digit in the credit card number, the digit is multiplied by 2.
* If the product is greater than 10 the digits of the product are added.
* This resultant digit is considered for the validation sum rather than the digit itself.
*
* Ref: https://www.geeksforgeeks.org/luhn-algorithm/
*/
const luhnValidation = (creditCardNumber) => {
let validationSum = 0
creditCardNumber.split('').forEach((digit, index) => {
let currentDigit = parseInt(digit)
if (index % 2 === 0) {
// Multiply every 2nd digit from the left by 2
currentDigit *= 2
// if product is greater than 10 add the individual digits of the product to get a single digit
if (currentDigit > 9) {
currentDigit %= 10
currentDigit += 1
}
}
validationSum += currentDigit
})
return validationSum % 10 === 0
}
const validateCreditCard = (creditCardString) => {
const validStartSubString = ['4', '5', '6', '37', '34', '35'] // Valid credit card numbers start with these numbers
if (typeof creditCardString !== 'string') {
throw new TypeError('The given value is not a string')
}
const errorMessage = `${creditCardString} is an invalid credit card number because `
if (isNaN(creditCardString)) {
throw new TypeError(errorMessage + 'it has nonnumerical characters.')
}
const creditCardStringLength = creditCardString.length
if (!((creditCardStringLength >= 13) && (creditCardStringLength <= 16))) {
throw new Error(errorMessage + 'of its length.')
}
if (!validStartSubString.some(subString => creditCardString.startsWith(subString))) {
throw new Error(errorMessage + 'of its first two digits.')
}
if (!luhnValidation(creditCardString)) {
throw new Error(errorMessage + 'it fails the Luhn check.')
}
return true
}
export { validateCreditCard }

View File

@ -0,0 +1,39 @@
import { validateCreditCard } from '../ValidateCreditCard'
describe('Validate credit card number', () => {
it('should throw error if card number is boolean', () => {
const invalidCC = true
expect(() => validateCreditCard(invalidCC)).toThrow(
'The given value is not a string'
)
})
it('returns true if the credit card number is valid', () => {
const validCreditCard = '4111111111111111'
const validationResult = validateCreditCard(validCreditCard)
expect(validationResult).toBe(true)
})
it('should throw an error on non-numeric character in given credit card number', () => {
const nonNumericCCNumbers = ['123ABCDEF', 'ABCDKDKD', 'ADS232']
nonNumericCCNumbers.forEach(nonNumericCC => expect(() => validateCreditCard(nonNumericCC)).toThrow(
`${nonNumericCC} is an invalid credit card number because ` + 'it has nonnumerical characters.'
))
})
it('should throw an error on credit card with invalid length', () => {
const ccWithInvalidLength = ['41111', '4111111111111111111111']
ccWithInvalidLength.forEach(invalidCC => expect(() => validateCreditCard(invalidCC)).toThrow(
`${invalidCC} is an invalid credit card number because ` + 'of its length.'
))
})
it('should throw an error on credit card with invalid start substring', () => {
const ccWithInvalidStartSubstring = ['12345678912345', '23456789123456', '789123456789123', '891234567891234', '912345678912345', '31345678912345', '32345678912345', '33345678912345', '38345678912345']
ccWithInvalidStartSubstring.forEach(invalidCC => expect(() => validateCreditCard(invalidCC)).toThrow(
`${invalidCC} is an invalid credit card number because ` + 'of its first two digits.'
))
})
it('should throw an error on credit card with luhn check fail', () => {
const invalidCCs = ['411111111111111', '371211111111111', '49999999999999']
invalidCCs.forEach(invalidCC => expect(() => validateCreditCard(invalidCC)).toThrow(
`${invalidCC} is an invalid credit card number because ` + 'it fails the Luhn check.'
))
})
})