Hong-Phuc Bui
2025-01-16 2889de7f0c2d587a17fbd322af57c29e84238620
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
/**
 * luhn.js
 *
 */
 
import {terminal} from "./dfhi.js";
 
window.main = function(...argv) {
    let sumEvent = 0n;
    let sumOdd = 0n;
    let credit = argv[0];
 
    let n = BigInt(credit.replace(/[-_\s+]/g, ''));
    while(n > 0) {
        sumOdd += (n % 10n);  // modulo division base 10 to get the last digit
        n = n/10n;            // integer division base 10 to shift credit number one digit to right
        let rest = n % 10n;   // get the second-last digit
        let p = rest*2n;
        sumEvent += (p > 9n) ? (p - 9n) : p;
        n = n/10n;
    }
    console.log({sumOdd, sumEvent});
    let sum =(sumEvent + sumOdd) % 10n;
    if (sum === 0n) {
        terminal.printl(`${credit} maybe OK`);
    } else {
        terminal.printl(`${credit} definitiv Wrong`);
    }
};