timoxley / functional-javascript-workshop

A functional javascript workshop. No libraries required (i.e. no underscore), just ES5.
2.05k stars 438 forks source link

Ex 7 recursion #184

Open pdewouters opened 7 years ago

pdewouters commented 7 years ago

My solution passed, but it is very different from the official solution.

    function reduce(arr, fn, initial) {
       if(!arr.length) return initial;
       fn(initial, arr[0], 0, arr);
       return reduce(arr.slice(1), fn, initial);
    }

    module.exports = reduce

I'm not really sure I understand more about recursion to be honest. The official solution still looks confusing to me - I wouldn't be able to come up with that myself.

gpetrioli commented 11 months ago

Your solution always passes 0 as the index to the fn. It should be the actual index in the array as the fn might want to use it internally.