From 85b0571d81072215da3d2c46e0b2234a224ae34c Mon Sep 17 00:00:00 2001 From: "@im_8055" <38890773+Bhargavishnu@users.noreply.github.com> Date: Thu, 4 Nov 2021 13:34:52 +0530 Subject: [PATCH] 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> --- DIRECTORY.md | 1 + String/ValidateCreditCard.js | 59 ++++++++++++++++++++++++++ String/test/ValidateCreditCard.test.js | 39 +++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 String/ValidateCreditCard.js create mode 100644 String/test/ValidateCreditCard.test.js diff --git a/DIRECTORY.md b/DIRECTORY.md index 427b4fad4..6c5da30d1 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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 diff --git a/String/ValidateCreditCard.js b/String/ValidateCreditCard.js new file mode 100644 index 000000000..9f9a35e51 --- /dev/null +++ b/String/ValidateCreditCard.js @@ -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 } diff --git a/String/test/ValidateCreditCard.test.js b/String/test/ValidateCreditCard.test.js new file mode 100644 index 000000000..12f0993ce --- /dev/null +++ b/String/test/ValidateCreditCard.test.js @@ -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.' + )) + }) +})