What pattern does the extended sum use?
The chain pattern (also called fluent interface or method chaining). Each method returns a new object with the updated value, allowing methods to be chained: calc(5).add(3).subtract(2).result(). Similar to jQuery and lodash.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in sum(1)(2)(3) with Add and Subtract Operations
Return an object with methods: function calc(a) { return { add: b => calc(a + b), subtract: b => calc(a - b), result: () => a }; }. Each method returns a new calc with the updated value. result() returns the final value.
Call the result() method: calc(5).add(3).result() returns 8. The result method returns the accumulated value and terminates the chain.
Yes. calc(10).add(5).subtract(3).multiply(2).result() returns 24. Each method returns a new calc with the updated value, so you can chain any combination of operations.
Still have questions?
Browse all our FAQs or reach out to our support team
