Skip to curriculum
Jump to the editor

5.6. Chain method calls (obj.a().b().c())

Easy
Topics
10–20 min

$("p").addClass("note").show(), query.where("age", 30).orderBy("name").limit(10), expect(x).not.toBe(y): chained calls read like a sentence because every method returns an object the next method can be called on. The simplest kind returns the very same object each time, so every call adds to one shared state.

Write amount(start) in amount.js. It returns an object for building an amount in the Indian number system with chained calls: amount().crore(5).lakh(15).thousand(45).value() is 51545000 (5,15,45,000).

Signature

JAVASCRIPT
function amount(start) // returns the chainable object

The returned object has these methods:

MethodAddsReturns
crore(count)count × 1,00,00,000the same object
lakh(count)count × 1,00,000the same object
thousand(count)count × 1,000the same object
hundred(count)count × 100the same object
ones(count)count × 1the same object
value()nothingthe total so far, a number
  • start: the starting total, 0 when missing.

Requirements

  1. The total starts at start (0 when missing); each unit method adds count of its unit.
  2. Each unit method returns the same object, so calls chain in any order, and a unit can be used more than once.
  3. value() returns the total and changes nothing: it can be called again, and the chain can go on after it.
  4. A chain stored in a variable half-way is the same object and keeps adding to the same total.
  5. Each call to amount() has its own total.
  6. A method taken off the object (const { lakh, value } = amount()) still works and still returns the chain object.
  7. Fractions and negative counts are allowed (lakh(1.5) adds 1,50,000).
  8. A count or a start that is not a number, or is NaN, throws a TypeError, and the total does not change.

Examples

JAVASCRIPT
amount().crore(5).lakh(15).thousand(45).value(); // 51545000
amount().ones(7).lakh(1).lakh(2).value();        // 300007: any order, repeats add up
amount(500).thousand(2).value();                 // 2500
JAVASCRIPT
const price = amount().lakh(2);
price.thousand(5);
price.value();                                   // 205000
JAVASCRIPT
const { lakh, value } = amount();
lakh(3);
value();                                         // 300000
amount().lakh("5");                              // TypeError

Environment

  • Plain JavaScript in a browser page; the tests call the global amount from amount.js.
  • Every built-in is allowed. Formatting the total as text ("5,15,45,000") is out of scope.

Hints

  1. Where can the total live so that every method of one object sees it, but two objects do not share it?
  2. If a method uses this, what is this when the method is called on its own, as lakh(3)?

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…