RECURSIVE return largest number in array

Tell us what’s happening:
Describe your issue in detail here.

I am trying to understand how this recursive method really works as I use another way to solve the problem.
This is based on the Return Largest Numbers in Arrays challenge.
I hope somebody can explain It to me.

  **Your code so far**

// recursive method that u don't really undestand
function largestOfFour(arr, finalArr = []) {
  return !arr.length
    ? finalArr
    : largestOfFour(arr.slice(1), finalArr.concat(Math.max(...arr[0])))
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Ahh, my bad, my copy/paste abilities aren’t as good as I thought they were. It works.

As for the recursive solution, what specifically are you having a hard time understanding? Do you understand the ternary operator? Do you understand what the slice method does? Do you understand what concat and Math.max do?

While the recursive solution does work I don’t think it is the best solution and I’m actually a little surprised that it is included as a possible solution in the hints. I also think it’s sort of a pseudo-recursive solution. Yes, it is calling itself, but it’s really just using a “global” array to store the solution and then returning it when it gets to the base case. This is recursion just for the sake of recursion and I doubt anyone would use this solution in a professional setting.

1 Like

The function returns finalArr, and that is an array that contains all the highest values from the matrix arr, and it does this by doing this:
largestOfFour(arr.slice(1), finalArr.concat(Math.max(...arr[0])))
until the handed in matrix is empty which it eventually will be as you are recursively calling the function with the first element being removed: arr.slice(1).

You can kind of think of the function being called as such:

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
largestOfFour([[13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]], [5]);
largestOfFour([[32, 35, 37, 39], [1000, 1001, 857, 1]], [5, 27]);
largestOfFour([[1000, 1001, 857, 1]], [5, 27, 39]);
largestOfFour([], [5, 27, 39, 1001]);
//after the above call the matrix will be empty and
//finalArr will look like [5, 27, 39, 10001]
//so that is the return value

A key thing to noticed is that I did not initially hand in anything for the finalArr argument as it has a default value and handing something in other than an empty array would lead to an incorrect return value.

One of the primary uses of recursion is a gain in speed at the cost of memory, but this use of recursion does not really do that as it is using the slice method and the spread operator which are both iterative processes.

1 Like

what does ‘!’ sign do in front of the arr.length.
I understand the ternary operator but the ! sign confusing me.

It is the logical NOT operator. It turns a truthy value false and a falsey value true. The length property returns the length of an array. So you can read !arr.length as “Not array length” or “array has not length” or “arr.length equals 0”.

1 Like

Now I get it … Thanks a lot for your help :smiley: