Skip to main content
Back to problems
#2797
Easy JavaScript

Partial function with placeholders

90.0% acceptance
Mar 31, 2026
11
4
Given a function fn and an array args, return a function partialFn. Placeholders "_" in the args should be replaced with values from restArgs starting from index 0. Any remaining values in the restArgs should be added at the end of the args. partialFn should return a result of fn. fn should be called with the elements of the modified args passed as separate arguments.

Solution

TypeScript
Time O(n)
Space O(1)
LeetCode
solution.ts
type JSONValue =
  | null
  | boolean
  | number
  | string
  | JSONValue[]
  | { [key: string]: JSONValue };
type Fn = (...args: JSONValue[]) => JSONValue;

function partial(fn: Fn, args: JSONValue[]): Fn {
  return function (...restArgs) {
  const result: JSONValue[] = [];
  let restIdx = 0;
  for (const arg of args) {
    if (arg === "_") {
    result.push(restArgs[restIdx++]);
    } else {
    result.push(arg);
    }
  }
  while (restIdx < restArgs.length) {
    result.push(restArgs[restIdx++]);
  }
  return fn(...result);
  };
}