Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda