-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2797-partial-function-with-placeholders.js
More file actions
39 lines (35 loc) · 1.06 KB
/
2797-partial-function-with-placeholders.js
File metadata and controls
39 lines (35 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 2797. Partial Function with Placeholders
* https://leetcode.com/problems/partial-function-with-placeholders/
* Difficulty: Easy
*
* Given a function fn and an array args, return a function partialFn.
*
* Placeholders "_" in the args should be replaced with values from restArgs starting from
* index 0. Any remaining values in the restArgs should be added at the end of the args.
*
* partialFn should return a result of fn. fn should be called with the elements of the
* modified args passed as separate arguments.
*/
/**
* @param {Function} fn
* @param {Array} args
* @return {Function}
*/
var partial = function(fn, args) {
return function(...restArgs) {
const finalArgs = [...args];
let restIndex = 0;
for (let i = 0; i < finalArgs.length; i++) {
if (finalArgs[i] === '_' && restIndex < restArgs.length) {
finalArgs[i] = restArgs[restIndex];
restIndex++;
}
}
while (restIndex < restArgs.length) {
finalArgs.push(restArgs[restIndex]);
restIndex++;
}
return fn(...finalArgs);
};
};