Skip to content

answers do not keep track of index/array in basic_recursion #2

@adam-beck

Description

@adam-beck

The methods you supplied use the index and array values in the callback function to the reduce method. However, they are not implemented and will always return 0 and the sliced array respectively (instead of the actual index and the entire inital values array).

One way I found to solve this is use a "static property"

reduce(arr, reducer, init) {
...
...
...

reduce.index = typeof reduce.index === 'number' ? reduce.index + 1: 0;
reduce.values = reduce.values || arr;
...
...
...
init = reducer(init, curr, reduce.index, reduce.values);
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions