JavaScript traps · Chapter 4 of 16

Floating-point money

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Everyday traps
  • 3 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.

Why you need this

JavaScript numbers are binary floating point. A fraction like one tenth has no exact binary form, the same way one third has no exact decimal form (0.3333...). So the number stored is the nearest one there is, and the tiny difference shows when you add:

console.log(0.1 + 0.2);
console.log(0.1 + 0.2 === 0.3);
console.log(0.7 + 0.1, 1.1 * 3);
console.log((0.1).toFixed(20));
0.30000000000000004
false
0.7999999999999999 3.3000000000000003
0.10000000000000000555

Rupees with paise are exactly this kind of fraction, and so is a 5% rate. Multiply and add a few of them and the total can land a hair above or below the true amount.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.js: short-change an exact payment on purpose

    Write reproduce() in repro.js. Build the ticket's cart, an array of lines { item, price, qty }: one bun maska at price 35 and one samosa at price 18. Call isFullyPaid(cart, 5565) from bill.js, where 5565 is what the customer paid in paise (₹55.65, the bill to the paisa), and resolve to what it gives.

    The check runs your reproduce() twice: beside the original bill.js it must resolve to false (an exact payment refused), and beside a fixed bill.js it must resolve to true. So it must really call isFullyPaid with that cart and 5565. No real waiting: reproduce() must finish within 2 seconds.

  2. Task 2

    bill.js: count the bill in whole paise

    Fix billTotal(lines) in bill.js so it counts in whole paise, never in rupees with decimals. Each line's amount is its price in paise (price is in rupees, so 12.5 is 1250 paise) times its qty. Each line's GST is 5% of that amount, rounded to the nearest paisa, a half rounding up (62.5 paise is 63). billTotal returns the sum of every line's amount and GST, a whole number: one bun maska at 35 and one samosa at 18 is 5565, and an empty cart is 0.

    isFullyPaid(lines, paidPaise) must then be true when the customer paid the bill or more, and false when even one paisa is missing. Keep paymentMessage, the 5% GST and the exports as they are. The check uses its own carts, some with prices in paise such as 12.5, 7.5 and 7.35, where rounding each line's GST gives a different bill from rounding the total at the end.

  3. Task 3

    bill.test.js: a test that would have caught it

    Write a vitest test file, bill.test.js, that would have caught this bug. The check runs your file three times: beside the original bill.js, where at least one test must fail; beside the fixed bill.js, where every test must pass; and beside a wrong fix that rounds the float total with toFixed before comparing, where at least one test must fail.

    The wrong fix gets the ticket's cart right, so test two carts: one paid exactly (such as the ticket's) that must count as paid, and one with prices in paise, such as a khari at 12.5 and an osmania biscuit at 7.5, where each line's GST rounds on its own: check its billTotal in whole paise, or that a payment one paisa short is refused. Each test must finish within 3 seconds.

What you will learn

  • repro.js: short-change an exact payment on purpose
  • bill.js: count the bill in whole paise
  • bill.test.js: a test that would have caught it

More in Everyday traps

Build "Floating-point money" yourself

Tapri runs on JavaScript, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.