Reduce in JavaScript and TypeScript
Complexity
Time complexity: O(n)
Space complexity: O(1)
Code
# Complexity
- Time complexity: O(n)
- Space complexity: O(1)

# Code
```javascript []
/**
 * @param {number[]} nums
 * @param {Function} fn
 * @param {number} init
 * @return {number}
 */
var reduce = function(nums, fn, init) {
    const length = nums.length;
    let i = 0

    for (i; i < length; i++) {
        init = fn(init, nums[i]);
    };

    return init;
};
```

```typescript []
type Fn = (accum: number, curr: number) => number

function reduce(nums: number[], fn: Fn, init: number): number {
    const length: number = nums.length;
    let i: number = 0;

    for (i; i < length; i++) {
        init = fn(init, nums[i]);
    };

    return init;
};
```
['TypeScript', 'JavaScript']