🟨 JavaScript
Hard
🕐 15 min

Extending the Number Type for Chained Operations

Goal: Extend the built-in Number type to support chained arithmetic operations.

Task Description

You need to add four methods to the Number prototype: plus(num), minus(num), multiply(num), and divide(num). These methods should allow for sequential arithmetic operations, as shown in the example:

const result = (10).plus(5).minus(3).multiply(2).divide(4);
// result should be 6

This task will help you gain a deeper understanding of prototypal inheritance in JavaScript, as well as the “fluent API” concept, where method calls can be chained together.

💡 Solution Hint
  1. To add methods to all numbers, use Number.prototype.
  2. Inside each method, this will refer to the number the method is being called on.
  3. Each method must return the result of the operation (a new number) so that the next method in the chain can work with it.
  4. Don’t forget to handle division by zero in the divide method.
👀 Solution
/**
 * We extend the Number prototype to add new methods.
 * `this` inside these methods will refer to the number
 * the method is being called on.
 */
 
// Method for addition
Number.prototype.plus = function(num) {
  // `this` is the current number (e.g., (2) in (2).plus(3))
  // We return the result of the addition to allow for chaining
  return this + num;
};
 
// Method for subtraction
Number.prototype.minus = function(num) {
  return this - num;
};
 
// Method for multiplication
Number.prototype.multiply = function(num) {
  return this * num;
};
 
// Method for division
Number.prototype.divide = function(num) {
  // Handle division by zero
  if (num === 0) {
    return Infinity;
  }
  return this / num;
};

Step-by-step code explanation:

  1. Number.prototype.plus = function(num) { ... };

    • We access Number.prototype to add a new plus method to all instances of Number (i.e., to all numbers).
    • The function takes one argument, num — the number to be added.
  2. return this + num;

    • this in the context of this method is the value of the number the method is called on. For example, in the expression (5).plus(3), this will be equal to 5.
    • We return the result of adding this and num. This is the key to building a chain: the result of (5).plus(3) (i.e., 8) becomes the new this for the next call in the chain.
  3. Similar methods minus, multiply, divide

    • They work on the same principle, performing the corresponding arithmetic operations.
  4. Handling division by zero

    • In the divide method, we added a check: if (num === 0). If we try to divide by zero, the function will return Infinity, which is consistent with standard JavaScript behavior.

Why it works this way:

  • Extensibility: Modifying the prototype allows you to add new functionality to all objects of a certain type without changing each object individually.
  • Readability: Method chaining (a fluent API) makes the code more declarative and easy to read, resembling natural language.
  • Caution: Although this is an excellent learning example, modifying the prototypes of built-in objects (Number, Array, String, etc.) is considered bad practice in large-scale projects. It can lead to conflicts with other libraries or future versions of JavaScript.

Examples

// Simple addition
(2).plus(3); // 5
 
// Chained operations
(2).plus(3).minus(1); // 4
 
// Full chain
(10).multiply(2).plus(5).divide(5).minus(2); // 3

Requirements

  • You must add four methods to Number.prototype: plus, minus, multiply, divide.
  • Each method must take one numeric argument.
  • Each method must return the result of the operation to enable method chaining.

🧑‍💻 It's not a bug! It's a feature!

The code editor is intentionally hidden on mobile.

Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.

📖 Now: Study the task, think through the solution. Act like a strategist.

💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!