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

146
Views
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 answers
Answer question

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 Report

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 Report

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 Report
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!