From 7a20549cc386ae8df6873d280823e6a227df9498 Mon Sep 17 00:00:00 2001 From: Bhargavishnu Date: Sun, 31 Oct 2021 01:32:28 +0530 Subject: [PATCH 1/9] Add credit card number validator - Validates the credit card number based on Luhn algorithm --- String/ValidateCreditCard.js | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 String/ValidateCreditCard.js diff --git a/String/ValidateCreditCard.js b/String/ValidateCreditCard.js new file mode 100644 index 0000000000..2ce9076b7a --- /dev/null +++ b/String/ValidateCreditCard.js @@ -0,0 +1,47 @@ +/** + * Validate a given credit card number + */ + +const validStartSubString = ['4', '5', '6', '37', '34', '35'] + +const luhnValidation = (creditCardNumber) => { + let validationSum = 0 + creditCardNumber.split('').forEach((digit, index) => { + let currentDigit = parseInt(digit) + if (index % 2 === 0) { + currentDigit *= 2 + if (currentDigit > 9) { + currentDigit %= 10 + currentDigit += 1 + } + } + validationSum += currentDigit + }) + + return validationSum % 10 === 0 +} + +const validateCreditCard = (creditCardString) => { + 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 } From 9974cd05ae376acef54f7fe0c62b9d43928d78f1 Mon Sep 17 00:00:00 2001 From: Bhargavishnu Date: Sun, 31 Oct 2021 01:34:48 +0530 Subject: [PATCH 2/9] Test Cases: ValidateCreditCard --- String/test/ValidateCreditCard.test.js | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 String/test/ValidateCreditCard.test.js diff --git a/String/test/ValidateCreditCard.test.js b/String/test/ValidateCreditCard.test.js new file mode 100644 index 0000000000..78a02dd917 --- /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 error on non numeric charecter 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.' + )) + }) +}) From 5e62db63c67b71cc77ecc0e1cb318df351eac64c Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 30 Oct 2021 20:11:14 +0000 Subject: [PATCH 3/9] Auto-update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 52edb49bad..b3842ae3c8 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -305,6 +305,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 From 58cc761d1f765c1f941ce1468b45ed449650dc58 Mon Sep 17 00:00:00 2001 From: Bhargavishnu Date: Sun, 31 Oct 2021 01:51:01 +0530 Subject: [PATCH 4/9] Fix: Spell check --- String/test/ValidateCreditCard.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/test/ValidateCreditCard.test.js b/String/test/ValidateCreditCard.test.js index 78a02dd917..12f0993cea 100644 --- a/String/test/ValidateCreditCard.test.js +++ b/String/test/ValidateCreditCard.test.js @@ -12,7 +12,7 @@ describe('Validate credit card number', () => { const validationResult = validateCreditCard(validCreditCard) expect(validationResult).toBe(true) }) - it('should throw error on non numeric charecter in given credit card number', () => { + 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.' From 8c3c65b39e41df5385b1babcfc294ce3a7810064 Mon Sep 17 00:00:00 2001 From: Bhargavishnu Date: Wed, 3 Nov 2021 19:47:20 +0530 Subject: [PATCH 5/9] Add references and move const inside function --- String/ValidateCreditCard.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/String/ValidateCreditCard.js b/String/ValidateCreditCard.js index 2ce9076b7a..14a37f3753 100644 --- a/String/ValidateCreditCard.js +++ b/String/ValidateCreditCard.js @@ -1,9 +1,8 @@ /** * Validate a given credit card number + * Ref: https://www.geeksforgeeks.org/luhn-algorithm/ */ -const validStartSubString = ['4', '5', '6', '37', '34', '35'] - const luhnValidation = (creditCardNumber) => { let validationSum = 0 creditCardNumber.split('').forEach((digit, index) => { @@ -22,6 +21,8 @@ const luhnValidation = (creditCardNumber) => { } const validateCreditCard = (creditCardString) => { + const validStartSubString = ['4', '5', '6', '37', '34', '35'] + if (typeof creditCardString !== 'string') { throw new TypeError('The given value is not a string') } From 5ba815af00796f490dd5425986aeccf6a6dc08e0 Mon Sep 17 00:00:00 2001 From: Bhargavishnu Date: Thu, 4 Nov 2021 00:23:15 +0530 Subject: [PATCH 6/9] Add comments --- String/ValidateCreditCard.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/String/ValidateCreditCard.js b/String/ValidateCreditCard.js index 14a37f3753..f02111b592 100644 --- a/String/ValidateCreditCard.js +++ b/String/ValidateCreditCard.js @@ -1,5 +1,6 @@ /** * Validate a given credit card number + * * Ref: https://www.geeksforgeeks.org/luhn-algorithm/ */ @@ -8,7 +9,9 @@ const luhnValidation = (creditCardNumber) => { 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 @@ -21,7 +24,7 @@ const luhnValidation = (creditCardNumber) => { } const validateCreditCard = (creditCardString) => { - const validStartSubString = ['4', '5', '6', '37', '34', '35'] + 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') From 5b43dc71a26d4a723e426a2a62a7218b51b6ee1a Mon Sep 17 00:00:00 2001 From: Bhargavishnu Date: Thu, 4 Nov 2021 00:25:37 +0530 Subject: [PATCH 7/9] Fix trailing spaces --- String/ValidateCreditCard.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/ValidateCreditCard.js b/String/ValidateCreditCard.js index f02111b592..593af29fd9 100644 --- a/String/ValidateCreditCard.js +++ b/String/ValidateCreditCard.js @@ -1,6 +1,6 @@ /** * Validate a given credit card number - * + * * Ref: https://www.geeksforgeeks.org/luhn-algorithm/ */ From fa78bf166bf9bfa8f9032a89378361a1bd7411f8 Mon Sep 17 00:00:00 2001 From: "@im_8055" <38890773+Bhargavishnu@users.noreply.github.com> Date: Thu, 4 Nov 2021 13:26:59 +0530 Subject: [PATCH 8/9] Add short description --- String/ValidateCreditCard.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/String/ValidateCreditCard.js b/String/ValidateCreditCard.js index 593af29fd9..18a3ce3cdf 100644 --- a/String/ValidateCreditCard.js +++ b/String/ValidateCreditCard.js @@ -1,6 +1,14 @@ /** * 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/ */ From f76e83f6f778f17f8b82f951a154879f01683f91 Mon Sep 17 00:00:00 2001 From: "@im_8055" <38890773+Bhargavishnu@users.noreply.github.com> Date: Thu, 4 Nov 2021 13:29:26 +0530 Subject: [PATCH 9/9] Remove trailing spaces --- String/ValidateCreditCard.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/String/ValidateCreditCard.js b/String/ValidateCreditCard.js index 18a3ce3cdf..9f9a35e518 100644 --- a/String/ValidateCreditCard.js +++ b/String/ValidateCreditCard.js @@ -3,10 +3,10 @@ * * 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, + * 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. + * 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/