Flip the first two arguments
Utility functions disagree about argument order — some take the collection first, some the callback. Rather than writing an adapter by hand every time, flip the one that does not fit.
Implement flip(fn). It returns a new function that forwards exactly as many arguments as it received, with the first two swapped, and returns fn's return value. If it receives fewer than two arguments, nothing is swapped and the arguments are forwarded unchanged — the wrapper never pads the call with undefined, so arguments.length inside fn always matches the number of arguments the wrapper was given.
What it has to do
- Swap the first two arguments and forward the rest in their original order.
- Forward exactly as many arguments as the wrapper received, never padding with
undefined. - Leave a single-argument call untouched.
- Return
fn's return value unchanged.
Your workspace
Ready to check it?
5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.