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

148
Visualizações
Returning a function with the arguments reversed

The code that I am practicing with is where I have a function called InReverse. This function accepts a function as an argument and returns a function. When the function returned is invoked, it reverses the order of the arguments.

When the returned functions are returned:

const catDog = ('cat', 'dog') => returns ('dog cat')

What I have rewritten out so far is:

function inReverse (func) {
return function (...arguments) {
    return arguments.map((element) => {
        element.reverse();
    });
}

}

Any guidance would be appreciated!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need to simply call the input function inside the newly created anonymous function.

For example this works:

function inReverse(f) {
     return function () {
         let args = [...arguments].reverse();
         return f.apply(this, args);
     }
}

So for example if you have subtract function like this:

function subtract(a, b) {
    return a-b;
}

subtract(1, 10); will be -9 as expected.

and inReverse(subtract)(1, 10) will be 9 as expected.

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório

0

Not sure why you're using map, just call reverse right on the arguments array. Also you weren't calling func:

function inReverse(func) {
    return function(...args) {
        return func(...args.reverse());
    };
}

(Notice that arguments is a reserved identifier in strict mode, you should name your parameter for something else)

about 4 years ago · Juan Pablo Isaza Relatório

0

Just reverse the arguments, and use func.apply(this, arr)

function inReverse(func) {

  return function() {

    var args = Array.prototype.slice.call(arguments)
    args = args.reverse();
    return func.apply(this, args);
  }
}

function div(a, b) {
  return a / b
}

console.log(div(1, 2))
console.log(inReverse(div)(1, 2))

about 4 years ago · Juan Pablo Isaza Relatório
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