Write calculator(start), whose methods can be chained: calculator(2).add(3).multiply(4).value().
calculator(start) returns a calculator object that holds a running total, starting at start. start defaults to 0.
add, subtract, multiply, and divide each take a number, update the total, and return the calculator, so calls can be chained. value() returns the total.
Dividing by zero throws a RangeError.
function calculator(start = 0) {
// your code
}
// calculator(2).add(3).multiply(4).value(); // 20
// calculator().add(10).subtract(4).divide(2).value(); // 3
// calculator(1).divide(0); // throws a RangeError
Solve it in the editor, with tests
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…