In this code, when the length of the array reaches 100, it DOES return 0.
function multiplos(arr) {
if(arr.length > 100) {return arr;}
else {
arr.push(arr[arr.length-1]*2);
return multiplos(arr);
}
}
console.log(multiplos([2]));
So why does it not return here as well, but instead returns the expected result? Instead, in the previous example I must return the array, because otherwise it returns 0.
function sum(nums) {
if (nums.length === 0) {
console.log("Sí llegé a 0");
return 0;
} else {
const [first, ...rest] = nums;
return first + sum(rest);
}
}
console.log(sum([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
Edition:
function multiplos(arr) {
if(arr.length > 100) {return 0;}
else {
arr.push(arr[arr.length-1]*2);
return arr + multiplos(arr);
}
}
console.log(multiplos([2]));
According to what Asier has answered me, it is because they add up and indeed here I see it, since apparently what is accumulating is what is before the addition, I mean "return **arr** + multiplos(arr)"
, even so I have the doubt if it really is like that , and why not return it as an array?
I think that all these doubts can be clarified at the same time, explaining well how recursion works. So, let's explain the basics:
A recursive function is one that calls itself. Let's leave indirect recursion aside (function A calls function B, which calls back to A) for simplicity.
A recursive function must have 2 well differentiated cases:
The base case, which is the trivial case where recursion is not used. For example, if we want to sum all the elements of an array, the base case is when the array is empty. In that case we return 0 and the execution ends there.
The recursive case, which is the non-trivial case. In this case, what is done is to break down the problem to be solved into smaller problems. Once the smaller problems have been solved, the original problem is solved with the union of all the solutions, either by adding, concatenating, multiplying... etc.
Once we understand the cases, we can move on to designing a solution to our problem. Take for example the case of adding all the elements of an array:
The base case would be when we want to add the elements of an empty array:
In that case the resulting value is zero, so let's put that in our function:
So we have the recursive case: If we have an array with N elements where N is greater than 0, let's take the first element. So we have on the one hand an element E and on the other hand an array with N-1 elements. We just reduced the problem, going from N to N-1. Therefore, the result would be adding the E plus the sum of the rest of the elements. Let's add the recursive case to our function and test it:
If, as in your first example, instead of returning
e + sumaArray(arrayConUnElementoMenos);
we had simply returnedsumaArray(arrayConUnElementoMenos);
, we would not be accumulating anything, we would simply return the base case (0) forgetting the intermediate results.Perhaps your second example is complicated to understand because of the notation used:
This line is equivalent to what I have put in my example:
And your last example fails in that you are adding an array to a number. Javascript, when you try to add data types that are not compatible, what it does is transform both types to text (strings) and concatenate them:
The first case returns 0 because the last call to the recursive function returns 0 and the rest of the calls return the result as is:
In the second case, the last call also returns 0, but the previous ones add a value to the result before returning it:
This is because the two recursive functions work differently:
The first function simply returns the result of the recursive function.
That is, it returns the last value, that is
0
, as it effectively turns out:The second function returns the first element plus the result of the recursive function, that's why it gives
45
: doing a debug this would be what would happenin the first instance the size of the array is
3
, then it returnsnow the array is of length
2
, so it returnsnow the array is of length
1
, so it returnsnow if it returns
0
, that is, adding the responsesthen , the result will be
3
, by the sum it accumulates.for it to return
0
, your function should be like thisWhy don't you implement the function like this?
It makes more sense than using recursion.