Intermediate Algorithm Scripting: Drop it Solution 4 is somehow strange

Hi I did the course and I get to Hint section in order to see different approaches.

Solution 4(Recursive) it has its own beauty but I don’t understand this i=0 argument.

function dropElements(arr, func, i = 0) {
  return i < arr.length && !func(arr[i])
    ? (dropElements(arr.slice(i + 1), func, i))
    : arr;
}

Instead you can remove it and works perfectly

function dropElements(arr, func) {
  return 0 < arr.length && !func(arr[0])
    ? (dropElements(arr.slice(1), func))
    : arr;
}

It should work just fine. This is a default parameter value: Default parameters - JavaScript | MDN

The default parameter value is not needed though.

Are you copy the second function and game you this error?
looks work to me here and I can’t find any i. Can you give your input. Look mine here Edit fiddle - JSFiddle - Code Playground

And to mention again its just my style not my function as I copied it from hints and just remove the default paremeter.

Thank you very much for the confirmation :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.