Goal: Extend the built-in
Numbertype to support chained arithmetic operations.
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 6This 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.
Number.prototype.this will refer to the number the method is being called on.divide method./**
* 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:
Number.prototype.plus = function(num) { ... };
Number.prototype to add a new plus method to all instances of Number (i.e., to all numbers).num — the number to be added.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.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.Similar methods minus, multiply, divide
Handling division by zero
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:
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.// 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); // 3Number.prototype: plus, minus, multiply, divide.Goal: Extend the built-in
Numbertype to support chained arithmetic operations.
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 6This 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.
Number.prototype.this will refer to the number the method is being called on.divide method./**
* 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:
Number.prototype.plus = function(num) { ... };
Number.prototype to add a new plus method to all instances of Number (i.e., to all numbers).num — the number to be added.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.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.Similar methods minus, multiply, divide
Handling division by zero
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:
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.// 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); // 3Number.prototype: plus, minus, multiply, divide.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!