Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Computing Check Digits With BigInt

Computing Check Digits With BigInt

In this lesson, you ported the line that computes each invoice’s RF creditor reference: RF, two check digits, and the reference, which is the customer number and the month. Python works out the check digits in one line, 98 - int(reference + "271500") % 97, on a 17-digit int, and Larder Lane’s reference is RF9140417202610.

  • Ported with Number() for int(), the line runs and prints RF8740417202610, where Python prints RF9140417202610. The 17-digit number is past 2 ** 53, so it isn’t exact: it logs as 40417202610271500, but the number it holds is 40417202610271504, Number.isSafeInteger() says false, and its remainder after 97 is 11, where Python’s is 7.
  • BigInt is JavaScript’s exact integer of any size. BigInt("40417202610271500") builds one from text, the way int() does, and prints as 40417202610271500n.
  • typeof answers "bigint" for it, and BigInts have the same +, -, *, % and comparisons as numbers.
  • Build BigInts from text, not from a number: BigInt(order.id) after a plain JSON.parse() keeps only the rounded ID.
  • With BigInt() for int(), the line stops with TypeError: Cannot mix BigInt and other types, use explicit conversions, at the remainder.
  • Python’s 98 and 97 are ints, but in JavaScript a plain 97 is a number, the counterpart of Python’s float. In Python, an int mixed with a float becomes a float, and with 97.0 the line gives 87.0, the wrong digits again. Turning a BigInt into a number would lose digits, and turning a number into a BigInt would lose any fraction, so JavaScript never combines the two in arithmetic: you choose.
  • An n after the digits writes a BigInt literal, and the fix gives every literal one: 98n - (BigInt(reference + "271500") % 97n) is 91n. The parentheses don’t change the order, since % comes first anyway. A template literal prints a BigInt without the n.
  • Python’s :02d becomes String(check) with a "0" in front when check < 10n. Bluebell Café’s November check is 8n, printed as 08. === never calls a BigInt and a number equal: 91n === 91 is false, where Python’s 91 == 91.0 is True, so compare with 91n.
  • Use BigInt where Python’s int goes past 2 ** 53 and the digits matter. Keep the whole calculation in BigInt, and convert at the edges. Everywhere else, a number is still the right type: BigInt has no fractions, so / drops the remainder, and the Math functions take numbers only.

Here’s the finished port of the reference lines:

Language: JavaScript
const INVOICE_MONTH = "202610";

function rfReference(reference) {
  const check = 98n - (BigInt(reference + "271500") % 97n);
  let digits = String(check);
  if (check < 10n) {
    digits = "0" + digits;
  }
  return `RF${digits}${reference}`;
}

console.log(rfReference("40417" + INVOICE_MONTH));
console.log(rfReference("40422" + INVOICE_MONTH));
console.log(rfReference("40436" + INVOICE_MONTH));
console.log(rfReference("40422" + "202611"));

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll port the line that computes each invoice’s check digits. Every invoice carries an RF creditor reference, so the café’s bank can match the payment.

00:10 It’s RF, two check digits, and the reference: the customer number and the month. Python works the check digits out in one line. It joins "271500", RF and two zeros written as digits, onto the reference.

00:28 Then it takes the remainder after dividing by 97 and subtracts it from 98. That’s a 17-digit int. The script prints three references, and Larder Lane’s check digits are 91.

00:43 In the port, Number() stands in for int(), and the rest of the line carries over. The f-string becomes a template literal. Python’s :02d padding waits until the end, since no October check needs it.

00:58 JavaScript runs it without an error and prints check digits 87, 31 and 10. Python’s are 91, 35 and 14. It’s the order numbers’ problem again: the 17-digit number is past 2 ** 53.

01:15 It logs with the same digits, but the number it holds ends in 504, not 500, and Number.isSafeInteger() says false for it. So its remainder after 97 comes out 11, where Python’s is 7.

01:33 JavaScript’s answer to Python’s int is BigInt, an exact integer of any size. In Node’s REPL, BigInt() builds one from the text, the way int() does.

01:44 It prints with an n on the end. Asked for its type, typeof answers 'bigint' for it. BigInts have the same +, -, *, % and comparisons as numbers.

01:58 Watch out: build a BigInt from text, not from a number: BigInt(order.id) after a plain JSON.parse() keeps only the rounded ID.

02:11 Back in the port, BigInt() replaces Number(), as int() is in the Python. What does the port print now? Your turn: pause the video and predict.

02:27 JavaScript stops with a TypeError: it can’t mix BigInt and other types. The stack trace points at line 4, at the remainder, before the subtraction even runs.

02:41 Python’s 98 and 97 are ints, but in JavaScript a plain 97 is a number, the counterpart of Python’s float. In Python, an int mixed with a float becomes a float: try 97.0 in Python’s line, and you get 87.0, the Number() port’s wrong digits.

03:02 JavaScript won’t choose for you: turning the BigInt into a number would lose digits, and turning a number into a BigInt would lose any fraction. So a BigInt and a number never combine in arithmetic.

03:16 The fix gives each literal an n after its digits, which makes it a BigInt literal. The parentheses only show the order: the remainder comes first anyway.

03:27 Now the check is 91n, Python’s 91. A template literal prints it without the n, and all three references match Python’s.

03:39 One piece is left: the :02d padding. String() turns the check into text, and an if adds a zero in front of any check under 10n, so it always has two digits.

03:52 A tidier way to pad text comes later. Bluebell’s November reference needs it: its check is 8, printed as 08. Watch out: === never calls a BigInt and a number equal: 91n === 91 is false, where Python’s 91 == 91.0 is True, so compare with 91n instead.

04:19 Use BigInt where Python’s int goes past 2 ** 53 and the digits matter. Build it from text or an n literal. Keep the whole calculation in BigInt, and convert only at the edge.

04:33 Everywhere else, a number is still the right type. BigInt has no fractions, so its division drops the remainder. And the Math functions take numbers only.

04:45 Next up is the exercise List October’s Refunds. You’ll start from a teammate’s port and print each refund with its order number, café and weight, as the Python script does.

Become a Member to join the conversation.