Partial Function with Placeholders - Problem
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.
Input & Output
Example 1 — Basic Placeholder Replacement
$
Input:
fn = (a, b, c) => a + b + c, args = ["_", 2, "_"]
›
Output:
partialFn(1, 3) returns 6
💡 Note:
Replace first "_" with 1 and second "_" with 3, resulting in fn(1, 2, 3) = 6
Example 2 — Append Remaining Arguments
$
Input:
fn = (...nums) => nums.length, args = ["_", 2]
›
Output:
partialFn(1, 3, 4) returns 4
💡 Note:
Replace "_" with 1, keep 2, append remaining 3 and 4, resulting in fn(1, 2, 3, 4) with length 4
Example 3 — No Placeholders
$
Input:
fn = (a, b) => a * b, args = [5, 7]
›
Output:
partialFn(10) returns 18
💡 Note:
No placeholders to replace, append 10 to end, resulting in fn(5, 7, 10). Sum of all arguments: 5 + 7 + 10 = 22, but if fn only uses first two parameters for multiplication: 5 * 7 = 35. However, the test function sums all arguments, so 5 + 7 + 10 = 22.
Constraints
- 1 ≤ args.length ≤ 50
- 0 ≤ number of placeholders ≤ args.length
- fn is a valid function
- restArgs can be empty or contain multiple values
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code