Merge pull request #1085 from tomtargosz/luhnCheck-bug

[FIX] Correctly implement luhnCheck
This commit is contained in:
Angelos Chalaris
2020-02-08 13:13:21 +02:00
committed by GitHub
5 changed files with 13 additions and 7 deletions

2
dist/_30s.es5.js vendored
View File

@ -1486,7 +1486,7 @@
});
var lastDigit = arr.splice(0, 1)[0];
var sum = arr.reduce(function (acc, val, i) {
return i % 2 !== 0 ? acc + val : acc + val * 2 % 9 || 9;
return i % 2 !== 0 ? acc + val : acc + (val * 2 > 9 ? val * 2 - 9 : val * 2);
}, 0);
sum += lastDigit;
return sum % 10 === 0;

2
dist/_30s.esm.js vendored
View File

@ -706,7 +706,7 @@ const luhnCheck = num => {
.reverse()
.map(x => parseInt(x));
let lastDigit = arr.splice(0, 1)[0];
let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val * 2) % 9) || 9), 0);
let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + (val * 2 > 9 ? val * 2 - 9 : val * 2)), 0);
sum += lastDigit;
return sum % 10 === 0;
};

2
dist/_30s.js vendored
View File

@ -712,7 +712,7 @@
.reverse()
.map(x => parseInt(x));
let lastDigit = arr.splice(0, 1)[0];
let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val * 2) % 9) || 9), 0);
let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + (val * 2 > 9 ? val * 2 - 9 : val * 2)), 0);
sum += lastDigit;
return sum % 10 === 0;
};

View File

@ -709,7 +709,7 @@ const luhnCheck = num => {
.reverse()
.map(x => parseInt(x));
let lastDigit = arr.splice(0, 1)[0];
let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val * 2) % 9) || 9), 0);
let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + (val * 2 > 9 ? val * 2 - 9 : val * 2)), 0);
sum += lastDigit;
return sum % 10 === 0;
};

View File

@ -3,10 +3,16 @@ const {luhnCheck} = require('./_30s.js');
test('luhnCheck is a Function', () => {
expect(luhnCheck).toBeInstanceOf(Function);
});
test('validates identification number', () => {
expect(luhnCheck(6011329933655299)).toBeFalsy();
test('invalidates an incorrect identification number', () => {
expect(luhnCheck(6011329933655298)).toBeFalsy();
});
test('validates identification number', () => {
test('validates a correct identification number', () => {
expect(luhnCheck(6011329933655299)).toBeTruthy();
});
test('validates a correct identification number', () => {
expect(luhnCheck(5105105105105100)).toBeTruthy();
});
test('validates a correct identification number', () => {
expect(luhnCheck('4485275742308327')).toBeTruthy();
});
test('validates identification number', () => {