Curried cart total that sums line items exactly to the cent
A checkout widget builds the order total one line item at a time: cartTotal(19.99)(5.01, 2)(-5)(). Each call adds a line, and the final empty call reads the total. Money also has to add up exactly: a customer who buys a $0.10 item and a $0.20 item expects to pay $0.30, not $0.30000000000000004.
Implement cartTotal.
cartTotal(price, quantity?) // => a chain function
cartTotal() // => 0
chain(price, quantity?) // => a new chain function
chain() // => the total, a number in dollars
price * quantity and returns a new chain function. quantity defaults to 1 when it is missing or undefined.cartTotal() returns 0.price is in dollars with at most two decimal places. It can be negative (a discount). Totals must be exact to the cent: add up whole cents, not floating-point dollars.price is not a finite number (for example '5', NaN, Infinity, or an explicit undefined), throw a TypeError.quantity is given and is not an integer >= 0, throw a RangeError.cartTotal(19.99)(5.01)(); // => 25
cartTotal(0.1)(0.2)(); // => 0.3
cartTotal(2.5, 4)(0.99)(); // => 10.99
cartTotal(30)(-7.5)(); // => 22.5
cartTotal(); // => 0
const base = cartTotal(12.5);
const withCoffee = base(3.25);
withCoffee(1.1)(); // => 16.85
withCoffee(); // => 15.75
base(); // => 12.5
cartTotal(undefined); // throws TypeError
cartTotal(4, 1.5); // throws RangeError