problems(js): add “2626. Array Reduce Transformation”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-05-12 17:32:21 +02:00
parent 56d6a05ce4
commit 0c23faf222
Signed by: mfocko
GPG key ID: 7C47D46246790496

View 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;
};