problems(js): add “2626. Array Reduce Transformation”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
56d6a05ce4
commit
0c23faf222
1 changed files with 15 additions and 0 deletions
15
problems/array-reduce-transformation.js
Normal file
15
problems/array-reduce-transformation.js
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
/**
|
||||||
|
* @param {number[]} nums
|
||||||
|
* @param {Function} fn
|
||||||
|
* @param {number} init
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
var reduce = function(nums, fn, init) {
|
||||||
|
let val = init;
|
||||||
|
|
||||||
|
for (let x of nums) {
|
||||||
|
val = fn(val, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
return val;
|
||||||
|
};
|
Loading…
Reference in a new issue