Restrict a function to one argument
Array.prototype.map calls its callback with the element, the index and the whole array. Most of the time the extra two are harmless, but hand it a function that accepts a second parameter — parseInt(string, radix) is the famous one — and you get nonsense: parseInt("7", 1) is NaN and parseInt("11", 2) is 3.
Implement unary(fn). It returns a new function that calls fn with exactly one argument, whatever else it was given: the wrapper's first argument. If the wrapper is called with no arguments it still calls fn with exactly one argument, whose value is undefined, so arguments.length inside fn is always 1. Return whatever fn returns.
What it has to do
- Pass only the first argument through to
fn, dropping any others. - Call
fnwith exactly one argument every time, even when the wrapper receives none. - Return
fn's return value unchanged.
Your workspace
Ready to check it?
4 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.