Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Learning and practice

AI wrote the discount function. Which receipt would you use to check it?

A basket worth $100.05 puts a ten per cent discount to the test. A one-cent difference reveals a decision you have to make before checking the code.

CKTIS Group

Dark navy CKTiS cover with a code icon and the title “Where did that last cent go?”

A shop offers ten per cent off baskets worth $100 or more. Spend exactly $100 and you pay $90. Now add five cents to the basket. Should the receipt say $90.04 or $90.05?

We asked AI to write a JavaScript function for this little shop. Before looking at the code, you can work out what you'd accept from it. That last digit is a good place to start.

Two perfectly reasonable receipts

One US dollar is 100 cents. Taking the basket total in whole cents gives us 10005 as the input. After ten per cent off, the customer owes nine tenths of that, which comes to 9004.5 cents. Round half up and the amount due is 9005 cents, or $90.05.

Try it from the cashier's end. The receipt might show the discount as its own line. Ten per cent of 10005 cents is 1000.5 cents. Round the discount to 1001, subtract it from 10005, and the bill is 9004 cents. You can follow every step of that calculation and still end up a cent away from the first receipt.

For this exercise the shop rounds the final amount due, with half a cent rounded up. That's the detail to include in the request to the model. We also gave it a price range of zero to one million cents and asked it to reject anything other than a whole number in that range. Here's the function.

function priceAfterDiscount(cents) {
  if (!Number.isInteger(cents) || cents < 0 || cents > 1_000_000) {
    throw new RangeError("0..1_000_000");
  }
  return cents >= 10_000 ? Math.round(cents * 9 / 10) : cents;
}

The threshold in the code is 10000 because the input is already in cents. If you pass in the number 100 to mean $100, the function will quite happily price a $1 basket. Its input checks can't catch a caller using the wrong unit. Naming the parameter helps the next person who wires it up to a till.

What to write in the test

On paper, price three baskets before running the function. $99.99 should cost precisely that. At $100 the discount starts, so the price drops to $90. The third basket, $100.05, owes $90.05 under the rule we picked above. Those handwritten amounts can go straight into the tests as whole cents.

import assert from "node:assert/strict";

assert.equal(priceAfterDiscount(9_999), 9_999);
assert.equal(priceAfterDiscount(10_000), 9_000);
assert.equal(priceAfterDiscount(10_005), 9_005);

assert.equal(priceAfterDiscount(0), 0);
assert.equal(priceAfterDiscount(1_000_000), 900_000);
for (const value of [-1, 1.5, "10000", 1_000_001]) {
  assert.throws(() => priceAfterDiscount(value), RangeError);
}

Copy both code blocks into discount.mjs and run node discount.mjs. When all checks pass, the command exits without printing anything. The expected prices are written out as numbers. If you calculate them by copying the discount formula into the test, a mistake in that formula can give you two identical wrong answers and a passing test.

Generating the function gave us a short piece of code we could run immediately. Choosing what should appear on the receipt was our job. For a quick check of how much the examples matter, change the threshold comparison from >= to >. The basket that started this story, $100.05, still costs $90.05. It's the boring receipt for exactly $100 that catches the change.