2666. Allow One Function Call
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : July 09, 2024
Last updated : July 09, 2024
Related Topics : N/A
Acceptance Rate : 86.53 %
/**
* @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
*/