Skip to content

Commit 180a21e

Browse files
day 10 task complete
1 parent 34323a2 commit 180a21e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

day10/leetcode2666.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// 2666. Allow One Function Call
2+
// URL -> https://leetcode.com/problems/allow-one-function-call/
3+
4+
/**
5+
* @param {Function} fn
6+
* @return {Function}
7+
*/
8+
var once = function (fn) {
9+
let call = false;
10+
return function (...args) {
11+
if (!call) {
12+
call = true;
13+
return fn(...args);
14+
}
15+
return undefined;
16+
}
17+
};
18+
19+
/**
20+
* let fn = (a,b,c) => (a + b + c)
21+
* let onceFn = once(fn)
22+
*
23+
* onceFn(1,2,3); // 6
24+
* onceFn(2,3,6); // returns undefined without calling fn
25+
*/
26+
27+
28+
// Try this at your own risk
29+
var onceAlternative = fn => (...args) => fn ? [fn(...args), fn = undefined][0] : fn;

0 commit comments

Comments
 (0)