Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda