Skip to curriculum
Jump to the editor

6.6. A calculator object shared across calls

Easy
Topics

This question is in

6. Functions and closures
15–20 min

A checkout form keeps a running amount: add an item, apply a discount, split the bill. Several buttons change the same amount, each wired up with its own handler, and the page shows the steps taken so far. The amount must be shared by every handler, but nothing else should be able to set it, and a bad input must not leave it half-changed.

Write createCalculator(start) in calculator.js. It returns an object whose methods all work on one private running total, and keep a history of the steps.

Signature

JAVASCRIPT
function createCalculator(start) // returns the calculator object
  • start: the starting total. A missing start (or undefined) counts as 0.
  • add(n), subtract(n), multiply(n), divide(n): change the total by n and return the new total.
  • total(): returns the current total.
  • history(): returns a new array of the steps taken since the start or the last reset, oldest first, each written as the method name, a space and the number, like "add 5" or "divide -2".
  • reset(): sets the total back to start, empties the history and returns the total.

Requirements

  1. The returned object has exactly the seven methods above.
  2. The total starts at start (0 when missing), and every operation works on the total left by the one before.
  3. Each operation returns the new total.
  4. An operation whose n is not a finite number (a string, NaN, Infinity, null, missing) throws a TypeError; divide(0) throws a RangeError. A step that throws changes neither the total nor the history.
  5. history() lists the steps in order, and returns a copy: changing that array does not change the calculator.
  6. reset() goes back to start and clears the history; later steps start from there.
  7. The methods share one total even when taken off the object, and ignore this.
  8. Calculators are independent, and the total cannot be changed by setting properties on the object.

Examples

JAVASCRIPT
const calc = createCalculator(10);
calc.add(5);       // 15
calc.multiply(2);  // 30
calc.divide(4);    // 7.5
calc.history();    // ["add 5", "multiply 2", "divide 4"]
calc.reset();      // 10
JAVASCRIPT
const { add, total } = createCalculator();
add(2);
add(3);
total(); // 5
JAVASCRIPT
const calc = createCalculator(8);
calc.divide(0);   // throws RangeError
calc.add("2");    // throws TypeError
calc.total();     // 8
calc.history();   // []

Environment

  • Plain JavaScript in a browser page; the tests call the global createCalculator from calculator.js.
  • start is a number or missing.
  • Results are ordinary JavaScript numbers: 0.1 + 0.2 is 0.30000000000000004, and no rounding is asked for.
  • Out of scope: undo, operator precedence and parsing expressions.

Hints

  1. Seven methods need the same total. Where can one variable live so that all seven, and nothing else, can reach it?
  2. Four operations do almost the same thing. Could one helper check the input, apply the step and record it?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…