LeetCode solutions

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

Read the problem on LeetCode View on GitHub

JavaScript

Accepted on LeetCode — runtime 41ms, memory 53.4MB, accepted 2025-12-31.

javascript
/**
 * @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
 */

Source