Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
How to get the result of executing a function inside it, make a transformation and return

The Add function is called by the user in the external environment. It is necessary that the called Add function is executed and the resulting value is converted to a number and returned as a response from the called Add function to the external environment.

function Ext() {
  let cs = 0;

  function pars(fn) {
    let newFn = fn.bind(null);

    function cal(...num) {
      newFn = newFn.bind(null, ...num);
      return cal;
    }

    cal.toString = () => newFn();

    return (...num) => {
      newFn = fn.bind(null);
      return cal(...num);
    };
  }

  const _add = pars((...num) => {
    num.forEach((item) => { cs += item; });
    return num.reduce((a, b) => a + b);
  });

  return {
    add: function add(...num) {
      if ([...num].length === 0) return add;
      return _add(...num);
    }
    /*
    add: function(...num) {
       function add(...num) {
        if ([...num].length === 0) return add;
        var promise = new Promise(function(resolve, reject) {
          let result = _add(...num);
          resolve(result);
        });
        return promise;
      }
      
      add(...num).then(function(result) { Number(result) });
    },
    */
  }
}

const ext = Ext();

console.log(ext.add()); //function
console.log(ext.add(1)); //1
console.log(ext.add(3, 1)()(3)); //7

console.log(typeof ext.add(1)); //function
let one = ext.add(1);
console.log(one === 1); //false
console.log(Number(one) === 1); //true
console.log(typeof one) //function
console.log(typeof 1) //number

That is, the add function externally returns a number, but it is a function by type. It is necessary that the function ultimately return exactly the number: console.log (typeof ext.add (1)) // Number P. S. The commented code in the question is an attempt to make the function return exactly the number.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!