forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2626-array-reduce-transformation.js
33 lines (30 loc) · 1.03 KB
/
2626-array-reduce-transformation.js
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
/**
* 2626. Array Reduce Transformation
* https://leetcode.com/problems/array-reduce-transformation/
* Difficulty: Easy
*
* Given an integer array nums, a reducer function fn, and an initial value init, return the final
* result obtained by executing the fn function on each element of the array, sequentially,
* passing in the return value from the calculation on the preceding element.
*
* This result is achieved through the following operations: val = fn(init, nums[0]),
* val = fn(val, nums[1]), val = fn(val, nums[2]), ... until every element in the array has been
* processed. The ultimate value of val is then returned.
*
* If the length of the array is 0, the function should return init.
*
* Please solve it without using the built-in Array.reduce method.
*/
/**
* @param {number[]} nums
* @param {Function} fn
* @param {number} init
* @return {number}
*/
var reduce = function(nums, fn, init) {
let result = init;
for (let i = 0; i < nums.length; i++) {
result = fn(result, nums[i]);
}
return result;
};