Chainable Calculator
EasyP2Coding
Implement calculator(initial = 0), which returns a chainable calculator object with these methods:
add(n),subtract(n),multiply(n): apply the operation to the running total and return the same calculator, so calls chain.value(): return the current total.
The calculator starts at initial (default 0). Each calculator keeps its own total. Reading value() does not change the total.
Example 1
- Input
- calculator(2).add(3).multiply(4).subtract(1).value()
- Output
- 19
- Why
- ((2 + 3) * 4) - 1
Constraints
- All numbers are finite.
- At most 10^4 chained calls.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 7. ⌘Enter runs, ⌘⇧Enter submits.