1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00

problems(js): add “2666. Allow One Function Call”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-05-12 17:33:13 +02:00
parent f7cdba6935
commit ddc1753842
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,23 @@
/**
* @param {Function} fn
* @return {Function}
*/
var once = function(fn) {
let 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
*/