We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 34323a2 commit 180a21eCopy full SHA for 180a21e
day10/leetcode2666.js
@@ -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