Search code examples
javascriptreactjsarrayscurrying

Write a Currying with multiple parameters


In a recent interview, I was asked to write a function that adds numbers and accepts parameters like this:

console.log(sum())
console.log(sum()())
console.log(sum(1));
console.log(sum(1)());
console.log(sum(1)(2)()); 
console.log(sum(1, 2)()); 
console.log(sum(1)(2)(3)()); 
console.log(sum(1)(2, 3)()); 
console.log(sum(1)(2, 3)); 
console.log(sum(1)(2)(3)(4)()); 
console.log(sum(1)(2, 3, 4)());
console.log(sum(1, 2, 3, 4)()); 
console.log(sum(1, 2, 3, 4)); 

But I am not able to solve below answer, how to get default as 0 in below output?

console.log(sum())
console.log(sum()())

Solution

  • Well solution should be something like this:

    const sum = (...args) => {
      const reducer = (acc, arg) => acc + arg;
      let total = args.reduce(reducer, 0)
      const add = (...args2) => {
        if (args2.length) {
          total = args2.reduce(reducer, total)
          return add
        }
        return total
      }
    
      add.toString = () => total;
      add.valueOf = () => total;
    
      return add
    }
    
    
    
    console.log(sum())
    console.log(sum()())
    console.log(sum(1));
    console.log(sum(1)());
    console.log(sum(1)(2)()); 
    console.log(sum(1, 2)()); 
    console.log(sum(1)(2)(3)()); 
    console.log(sum(1)(2, 3)()); 
    console.log(sum(1)(2, 3)); 
    console.log(sum(1)(2)(3)(4)()); 
    console.log(sum(1)(2, 3, 4)());
    console.log(sum(1, 2, 3, 4)()); 
    console.log(sum(1, 2, 3, 4)); 

    It seems that I cannot quiet find a function that needs to be overriden in order to get nice output.

    So in case of console.log(sum()) you will get ƒ add(). But if you write like this console.log("" + sum()) you will get 0 in console. console.log is not calling toString automatically :( probably some other method needs to be overriden additionally.