Allow One Function Call - Problem
Given a function fn, return a new function that is identical to the original function except that it ensures fn is called at most once.
Requirements:
- The first time the returned function is called, it should return the same result as
fn - Every subsequent time it is called, it should return
undefined
Input & Output
Example 1 — Basic Addition Function
$
Input:
fn = (a,b) => a + b, calls = [[1,2], [3,4]]
›
Output:
[3, undefined]
💡 Note:
First call: fn(1,2) executes and returns 1+2=3. Second call: fn(3,4) doesn't execute, returns undefined.
Example 2 — Single Call Only
$
Input:
fn = (a,b,c) => a + b + c, calls = [[2,3,6]]
›
Output:
[11]
💡 Note:
Only one call made: fn(2,3,6) = 2+3+6 = 11. Function executes normally on first call.
Example 3 — Multiple Blocked Calls
$
Input:
fn = (a,b) => a * b, calls = [[5,7], [2,4], [1,3]]
›
Output:
[35, undefined, undefined]
💡 Note:
First call: fn(5,7) = 5*7 = 35. Second and third calls return undefined as function already executed once.
Constraints
- fn is a valid JavaScript function
- fn can be called with 0 or more arguments
- calls.length ≥ 1
- 0 ≤ calls[i].length ≤ 10
Visualization
Tap to expand
Understanding the Visualization
1
Input
Original function fn and array of call arguments
2
Process
Create wrapper function with execution state tracking
3
Output
First call returns fn result, subsequent calls return undefined
Key Takeaway
🎯 Key Insight: Closure provides persistent state to track function execution across calls
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code