Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise [7] - Basic Recursion #172

Open
joaocferreira opened this issue Dec 31, 2016 · 1 comment
Open

Exercise [7] - Basic Recursion #172

joaocferreira opened this issue Dec 31, 2016 · 1 comment

Comments

@joaocferreira
Copy link

function reduce(arr, fn, initial) {
  if (arr[0]) {
    initial = fn(initial, arr.splice(0, 1));
    return reduce(arr, fn, initial);
  } else {
    return initial;
  }
};

I did this solution that seems to do what is expected but fails on the tests. Can anyone point what i may be missing?

@orthotypos
Copy link

@joaocferreira .splice() method will return an array. But in general, splice is not really good method for "functional-javascript-workshop" (it mutates the original array).

Try using .slice() instead, like:

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

This will pass you a test, but technically solution is not 100% complete, as callback fn is not satisfying requirement:

fn: Function to use as the reduction step. Like regular Array#reduce, this function must be passed previousValue, currentValue, index and the array we're iterating over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants