2796. Allow One Function Call
My accepted JavaScript solution to LeetCode problem 2796, Allow One Function Call, running in 41ms.
- Difficulty: Easy
- JavaScript
- Runtime 41ms
- Memory 53.4MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
JavaScript
Accepted on LeetCode — runtime 41ms, memory 53.4MB, accepted 2025-12-31.
/**
* @param {Function} fn
* @return {Function}
*/
var once = function(fn) {
let called = false;
return function(...args) {
if (!called) {
called = true;
return fn(...args);
}
return undefined;
};
};
/**
* let fn = (a,b,c) => (a + b + c)
* let onceFn = once(fn)
*
* onceFn(1,2,3); // 6
* onceFn(2,3,6); // returns undefined without calling fn
*/