Skip to content

Latest commit

 

History

History
78 lines (63 loc) · 1.41 KB

_2666. Allow One Function Call.md

File metadata and controls

78 lines (63 loc) · 1.41 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 09, 2024

Last updated : July 09, 2024


Related Topics : N/A

Acceptance Rate : 86.53 %


Solutions

JavaScript

/**
 * @param {Function} fn
 * @return {Function}
 */
var once = function(fn) {
    var called = false;
    return function(...args){
        if (called) {
            return undefined;
        }
        called = true;
        return fn(...args);
    }
};

/**
 * 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
 */
/**
 * @param {Function} fn
 * @return {Function}
 */
var once = function(fn) {
    var called = false;
    return function(...args){
        if (!called) {
            called = true;
            return fn(...args);
        }
    }
};

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