Nodejs Number Add add(num)

Here you can find the source of add(num)

Method Source Code

/* Prompt/*from   ww w  .  j  a  va2 s  .co  m*/
Method chaining is a very interesting way to keep your program clean.

As a part of this Kata, you need to create functions such that one could evaluate the following expression:

(3).add(5).multiply(2)
The above expression evaluates to be 16.

You need to implement the following methods:

add
subtract
multiply
divide
square
After you're done, one could chain these five methods to create chains of almost any length.
*/

// Solution
Number.prototype.add = function (num) {
  return this + num;
};

Number.prototype.subtract = function (num) {
  return this - num;
};

Number.prototype.multiply = function (num) {
  return this * num;
};

Number.prototype.divide = function (num) {
  return this / num;
};

Number.prototype.square = function () {
  return this * this;
};

Related

  1. add(arg)
    Number.prototype.add = function(arg){
      var r1, r2, m;
      try{r1 = this.toString().split(".")[1].length}catch(e){r1=0}
      try{r2 = arg.toString().split(".")[1].length}catch(e){r2=0}
      m = Math.pow(10, Math.max(r1, r2))
      return (this*m+arg*m)/m
    Number.prototype.sub = function(arg){
      return this.add(-arg)
    ...
    
  2. add(n)
    Number.prototype.add = function(n){
      return this+n
    Number.prototype.subtract = function(n){
      return this-n
    Number.prototype.multiply = function(n){
      return this*n
    Number.prototype.divide = function(n){
      return this/n
    Number.prototype.square = function(n){
      var _ = this;
      return Math.pow(_,2)
    Number.prototype.add      = function(n){ return this+n }
    Number.prototype.subtract = function(n){ return this-n }
    Number.prototype.multiply = function(n){ return this*n }
    Number.prototype.divide   = function(n){ return this/n }
    Number.prototype.square   = function(){ return this*this }
    
  3. add(n) return this+n }
    Number.prototype.add      = function(n){ return this+n }
    Number.prototype.subtract = function(n){ return this-n }
    Number.prototype.multiply = function(n){ return this*n }
    Number.prototype.divide   = function(n){ return this/n }
    Number.prototype.square   = function(){ return this*this }
    
  4. add(num)
    Number.prototype.add = function(num) {
      return this + num;
    var n = 5;
    assert(n.add(3) == 8, 'It works when the number is in a variable.');
    assert((5).add(3) == 8, 'Also works if a number is wrapped in parentheses.');
    assert(5.add(3) == 8, 'What about a simple literal?');
    
  5. add(num)
    Number.prototype.add = function(num)
        return this+num;
    };
    describe("6.13", function () {
        it("It works when the number is in a variable.", function () {
             expect(n.add(3)).toEqual(8);
        });
        it("It works when the number is in a parentheses.", function () {
    ...
    
  6. add(x)
    Number.prototype.add = function(x){
      return this + x;
    };
    Number.prototype.sub = function(x){
      return this - x;
    };
    console.log("***>(4).add(3).sub(1):", (4).add(3).sub(1));