Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiply 3 numbers in javascript in closure way

Tags:

javascript

function fn() {
    // implementation
}
fn(2,3,4); // 24
fn(2,3)(4); // 24
fn(2)(3)(4); // 24

I am not able to achieve the multiplication in the example shown above. How am I able to achieve it? Any kind of help is highly appreciated!

like image 540
vennapusa Avatar asked Jun 04 '18 03:06

vennapusa


People also ask

How do you write a closure in JavaScript?

This is called a JavaScript closure. It makes it possible for a function to have "private" variables. The counter is protected by the scope of the anonymous function, and can only be changed using the add function. A closure is a function having access to the parent scope, even after the parent function has closed.

What is closure function in JavaScript?

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.

How do you multiply values in JavaScript?

The multiplication operator ( * ) multiplies numbers.

Which of the following are closures in JavaScript?

Explanation: In JavaScript, closures are created every time a function is created, at function creation time. Technically, all JavaScript functions are closures: they are objects, and they have a scope chain associated with them. 5. Which of the following uses a lot of CPU cycles?


Video Answer


2 Answers

You could have a function that returns itself with bound arguments if the total number of arguments provided so far is less than 3:

const multiply = (...args) => (
  args.length < 3
  ? multiply.bind(null, ...args)
  : args.reduce((a, b) => a * b)
);
console.log(multiply(2, 3, 4));
console.log(multiply(2, 3)(4));
console.log(multiply(2)(3)(4));
console.log(multiply(2)(3, 4));
like image 184
CertainPerformance Avatar answered Sep 25 '22 01:09

CertainPerformance


    function curry(func,args,space) {
        var n  = func.length - args.length; //arguments still to come
        var sa = Array.prototype.slice.apply(args); // saved accumulator array
        function accumulator(moreArgs,sa,n) {
            var saPrev = sa.slice(0); // to reset
            var nPrev  = n; // to reset
            for(var i=0;i<moreArgs.length;i++,n--) {
                sa[sa.length] = moreArgs[i];
            }
            if ((n-moreArgs.length)<=0) {
                var res = func.apply(space,sa);
                // reset vars, so curried function can be applied to new params.
                sa = saPrev;
                n  = nPrev;
                return res;
            } else {
                return function (){
                    // arguments are params, so closure business is avoided.
                    return accumulator(arguments,sa.slice(0),n);
                }
            }
        }
        return accumulator([],sa,n);
    }
    // now you can define any type of function, add, Mul etc...
    function add (a,b,c){
          if (arguments.length < this.add.length) {
            return curry(this.add,arguments,this);
          }
          return a+b+c;
    }

    function mul (a,b,c){
        if (arguments.length < this.mul.length) {
          return curry(this.mul,arguments,this);
        }
        return a*b*c;
    }

    // Multiplication
    console.log("Multiplication Sample");
    console.log(mul(2,3,4))         // 24
    console.log(mul(2,3)(4))        // 24
    console.log(mul(2)(3)(4))       // 24
    console.log(mul()(1,2,4));      // 8
    console.log(mul(1)(2)(5));      // 10
    console.log(mul(1)()(2)()(6));  // 12

    // Addition
    console.log("Addition Sample");
    console.log(add(2,3,4))         // 9
    console.log(add(2,3)(4))        // 9
    console.log(add(2)(3)(4))       // 9
    console.log(add()(1,2,4));      // 7
    console.log(add(1)(2)(5));      // 8
    console.log(add(1)()(2)()(6));  // 9

Reference : http://www.crockford.com/javascript/www_svendtofte_com/code/curried_javascript/index.html

like image 27
Hiteshdua1 Avatar answered Sep 22 '22 01:09

Hiteshdua1