Build a price calculation tool
Does changing calculation order preserve the contract?
Start with a concrete question
An original synthetic invoice has base prices 10,20,30. The contract adds percentage tax to each base price, then an untaxed flat fee. Implement the rule in the linked code runner.
Try the initial inputs, then write a prediction before changing one setting. All calculations run locally on your CPU.
What do you expect?
Predict whether taxing a fee accidentally matters when fee or tax is zero.
Change one thing. Inspect what happens.
- Calculate the first item manually, then inspect the loop trace.
- Switch implementation order and find cases that hide the bug.
- Copy the starter in the explanation into the code runner; repair it and print all three charges and their total.
Your settingsMechanism
Calculation belowEvidence
Compare outcomes
Compare the current results
Every bar starts at zero and uses the same numeric scale in this chart. The upper bound automatically adjusts to the largest current value (at least 1), so compare the numbers when changing inputs. Units and quantities come from each label; different kinds of quantity are not interchangeable. Displayed values are rounded to at most six significant digits.Follow the calculation
- Price 10: price × (1 + rate) + fee = 13 credits.
- Price 20: price × (1 + rate) + fee = 24 credits.
- Price 30: price × (1 + rate) + fee = 35 credits.
The contract taxes the item, then adds an untaxed fee. Reordering operations changes the result; named intermediate values make it inspectable.
Implement the invoice tool
Repair the charge function so the fee is untaxed. Print each charge and the total. Then add a zero-tax test and explain why that test alone cannot detect the bug. Expected default outputs: 13, 24, 35, 72. Use Run; a printed match is practice, not a proof of correctness.
237/8,000 characters. Drafts save locally and are included in backups. Importing or opening a draft never runs it.
Bounded JavaScript subset: variables, primitive arrays, conditions, loops, functions and print. No browser objects, network, imports or eval. Maximum 10,000 operations, 64 array items, call depth 16, strings of 256 characters, 40 output lines / 4,000 output characters and 160 trace frames.
Compare with my earlier draft
function charge(price, rate, fee) {
return (price + fee) * (1 + rate);
}
const prices = [10, 20, 30];
let total = 0;
for (const price of prices) {
const value = charge(price, 0.1, 2);
print(value);
total += value;
}
print(total);Explain what you changed and why in your observation notes. Text differences do not establish quality.
Inspect an example solution after your attempt
function charge(price, rate, fee) {
return price * (1 + rate) + fee;
}
const prices = [10, 20, 30];
let total = 0;
for (const price of prices) {
const value = charge(price, 0.1, 2);
print(value);
total += value;
}
print(total);This is one worked example. Test another input and explain its limitations.
Captures keep your inputs, prediction and observation. Results are recalculated from those inputs. Resetting inputs keeps your captures and challenge history.
Explain the mechanism
Build a price calculation tool
An original synthetic invoice has base prices 10,20,30. The contract adds percentage tax to each base price, then an untaxed flat fee. Implement the rule in the linked code runner.
Predict before changing inputs
Predict whether taxing a fee accidentally matters when fee or tax is zero.
Understand the mechanism
A tool needs an input contract and an output contract. Here the fee is explicitly untaxed; putting it inside the multiplication violates the rule.
For price 10, tax 10% and fee 2, the correct result is 10×1.1+2=13. The incorrect taxed-fee expression produces 12×1.1=13.2.
Use named intermediate values and test more than one case. Default total is 13+24+35=72 credits.
Calculate it yourself
Final charge = base price × (1 + tax fraction) + untaxed fee.
Guided investigation
Calculate the first item manually, then inspect the loop trace.
Switch implementation order and find cases that hide the bug.
Copy the starter in the explanation into the code runner; repair it and print all three charges and their total.
Assumptions and limitations
Rates and money are fictional; this is not tax advice or a production currency library.
JavaScript uses binary floating-point. Real financial tools need explicit rounding and currency-unit rules.
Further investigations
High school: Build a small event-cost calculator and show sample inputs and outputs.
College: Implement a pure function and tests for zero fee, zero rate and multiple items.
Working professionals: Document a pricing interface contract including rounding, invalid input and ownership.
Your implementation or data task
Repair the charge function so the fee is untaxed. Print each charge and the total. Then add a zero-tax test and explain why that test alone cannot detect the bug. Expected default outputs: 13, 24, 35, 72. Use Run; a printed match is practice, not a proof of correctness.
Use the editable workspace on this page. Its output is practice evidence; explain why it satisfies the contract and test a fresh case.
Try a fresh case
A new item costs 40, tax is 20%, and an untaxed fee is 3. What is its charge?
This is a public practice check. Retries support learning; matching a number does not certify independent proficiency.
Take the investigation further
Choose any extension. These are project prompts for you to carry out; they are not automatically assessed.
School investigation
Build a small event-cost calculator and show sample inputs and outputs.
College investigation
Implement a pure function and tests for zero fee, zero rate and multiple items.
Workplace investigation
Document a pricing interface contract including rounding, invalid input and ownership.
Keep the evidence, choose your next step
Completion records your own decision that you have worked through this investigation. Practice checks and independent project evidence are separate.