problems(js): add “2666. Allow One Function Call”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
f7cdba6935
commit
ddc1753842
1 changed files with 23 additions and 0 deletions
23
problems/allow-one-function-call.js
Normal file
23
problems/allow-one-function-call.js
Normal 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
|
||||||
|
*/
|
Loading…
Reference in a new issue