Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Method Chaining

Object-Oriented Programmingmedium

Course · Section 14: Object-Oriented Programming (OOP) With JavaScript · Lecture 237: Chaining Methods

Using a `Calc` class whose `add` and `mul` methods return `this`, implement `calcChain(a, b)` that starts at 0, adds `a`, multiplies by `b`, and returns the result via chaining.

Sample tests

Input: calcChain(5, 2)
Output: 10
Input: calcChain(0, 5)
Output: 0

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • A method that returns nothing breaks the chain.
Approach & explanation (try first)

Each mutating method returns this, so calls can be chained fluently.

Loading...
⌘/Ctrl + Enter

Run your code to see results.