-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdamm.js
More file actions
40 lines (35 loc) · 913 Bytes
/
Copy pathdamm.js
File metadata and controls
40 lines (35 loc) · 913 Bytes
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
33
34
35
36
37
38
39
40
var table = [
[0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
[7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
[4, 2, 0, 6, 8, 7, 1, 3, 5, 9],
[1, 7, 5, 0, 9, 8, 3, 4, 2, 6],
[6, 1, 2, 3, 0, 4, 5, 9, 7, 8],
[3, 6, 7, 4, 2, 0, 9, 5, 8, 1],
[5, 8, 6, 9, 7, 2, 0, 1, 3, 4],
[8, 9, 4, 5, 3, 6, 2, 0, 1, 7],
[9, 4, 3, 8, 6, 1, 7, 2, 0, 5],
[2, 5, 8, 1, 4, 3, 6, 7, 9, 0]
];
function generate(input) {
if (typeof input !== 'string') {
throw new Error('Input must be a string.');
}
if (!input.match(/^\d+$/)) {
throw new Error('Input must only contain digits.');
}
var row = 0;
for(var i = 0; i < input.length; i++) {
var col = input.charAt(i);
row = table[row][col];
}
return row.toString();
}
function append(input) {
return input + generate(input);
}
function verify(input) {
return generate(input) === '0';
}
exports.generate = generate;
exports.append = append;
exports.verify = verify;