-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (25 loc) · 976 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict';
/*
Code based on https://en.wikipedia.org/wiki/Luhn_mod_N_algorithm
with minimal modifications.
*/
module.exports = function calculateLuhnModN(codePointFromCharacter, characterFromCodePoint, n, input) {
var factor = 2;
var sum = 0;
// Starting from the right and working leftwards is easier since
// the initial "factor" will always be "2"
for (var i = input.length - 1; i >= 0; i--) {
var codePoint = codePointFromCharacter(input[i]);
var addend = factor * codePoint;
// Alternate the "factor" that each "codePoint" is multiplied by
factor = (factor == 2) ? 1 : 2;
// Sum the digits of the "addend" as expressed in base "n"
addend = Math.floor(addend / n) + (addend % n);
sum += addend;
}
// Calculate the number that must be added to the "sum"
// to make it divisible by "n"
var remainder = sum % n;
var checkCodePoint = (n - remainder) % n;
return characterFromCodePoint(checkCodePoint);
}