最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to multiply numbers in a closure - Stack Overflow

programmeradmin1浏览0评论

How to implement a locking function for multiplying an arbitrary number of numbers.

Example of a call:

multiply(1)(2)(3)(4)(5) // 120

To acplish this task, it is necessary to redefine the toString method for the internal function, which should return the accumulated result, but I had result NaN.

function Multiply(arguments) {
  for (var i = 0; i < arguments.length; i++) {
    var number = arguments.length[i];
  }
  return function(res) {
    return number * res.valueOf();
  };
}
console.log(Multiply(5)(5)(6)(8));

How to implement a locking function for multiplying an arbitrary number of numbers.

Example of a call:

multiply(1)(2)(3)(4)(5) // 120

To acplish this task, it is necessary to redefine the toString method for the internal function, which should return the accumulated result, but I had result NaN.

function Multiply(arguments) {
  for (var i = 0; i < arguments.length; i++) {
    var number = arguments.length[i];
  }
  return function(res) {
    return number * res.valueOf();
  };
}
console.log(Multiply(5)(5)(6)(8));

Share Improve this question edited Jul 25, 2021 at 10:37 Penny Liu 17.5k5 gold badges86 silver badges108 bronze badges asked May 6, 2018 at 16:36 Igor ShvetsIgor Shvets 5476 silver badges16 bronze badges 4
  • 3 arguments.length[i] doesn't make sense since length returns an integer. Read up on how to create a "curried" function or the term "currying" – charlietfl Commented May 6, 2018 at 16:40
  • Indeed. You only have one argument to Multiply. – Amadan Commented May 6, 2018 at 16:41
  • 1 stackoverflow./questions/33901793/… stackoverflow./questions/46286531/… stackoverflow./questions/48576573/… stackoverflow./questions/35039020/… – VLAZ Commented May 6, 2018 at 16:45
  • Possible duplicate of Is it possible to write continuous nested functions in JavaScript? – Redu Commented May 6, 2018 at 16:55
Add a ment  | 

3 Answers 3

Reset to default 5

First of all do not use arguments as parameter in a function, because this variable is available in functions as array like object for the arguments of the function (arguments object).

Then you need to have an inner function m which uses the arguments and calculates the product and returns the function itself.

The inner function gets a toString method for getting the final result.

At last you need to call the inner function with all arguments of the outer function.

A small hint, take only a lower case letter for not instanciable function.

function multiply(...args) {
    function m(f, ...a) {
        p *= f;
        if (a.length) {
            m(...a);
        }
        return m;
    }

    var p = 1;            // neutral value for multiplication
    m.toString = _ => p;
    return m(...args);
}

console.log(multiply(5)(5)(6)(8));
console.log(multiply(2, 3, 4)(5)(6, 7));

  1. The function returned by Multiply should return itself after each call.
  2. valueOf should be assigned to that function not to its argument which will be a number.
  3. Multiply should call the inner function with its argument (the initial number).
  4. No need to use the arguments object as there will always be one argument.

function Multiply(initialNum) {
    var product = 1;

    function fn(num) {
        product *= num;
        return fn;
    };

    fn.valueOf = function() { return product; };

    return fn(initialNum);
}

console.log(0 + Multiply(5)(5)(6)(8));

Note: The 0 + in console.log is to assure that valueOf will be called as the SO snippett console doesn't seem to work properly.

You can implement it by overwriting Function#toString method which would call internally in major caser(for eg: while using with alert() function, string concatenation, etc...).

function Multiply(arg) {
  // calcumate the multiplication result
  var res = (this.value || 1) * arg,
    // bind this argument as an object which contains previous result
    returnFn = Multiply.bind({
      value: res
    })

  // overwrite toString method to return the current result
  returnFn.toString = function() {
    return res;
  }

  // return the function 
  return returnFn;
}


console.log(Multiply(5)(5)(6)(8));

发布评论

评论列表(0)

  1. 暂无评论