Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
brandonlind2
7,823 PointsWhy isn't this recursion function working?
//I'm trying to get it to add to the ary, but it only adds the initial argument that is passed? How can I do this and why isn't what I did not working so I understand recursion better.
function recur(arg){
let ary=[];
ary.push(arg)
if(arg< 10){
recur(arg+1);
}
console.log(arg);
return ary;
}
1 Answer
Umesh Ravji
42,386 PointsHey there, in short, you aren't doing anything with the value that is returned from the next recursive call.
Each time you call the recursive method, the value being returned from the next recursive call is essentially being thrown away (or lost). You aren't doing anything with it. In order to build up the resulting array, you have to keep joining the result of the next call to the current one.
Take a look at this example. It sums a number from whatever is entered, for example 5 would sum 5 + 4 + 3 + 2 + 1..
function recursive_sum(n) {
var x;
if (n === 1) {
x = 1;
} else {
x = n;
x += recursive_sum(n - 1);
}
return x;
}
var s = recursive_sum(20);
console.log(s); // 210
Now this is what your code is doing, you aren't including the result. That is why you are only getting the first one back.
function recursive_sum2(n) {
var x;
if (n === 1) {
x = 1;
} else {
x = n;
recursive_sum(n - 1); // see the difference
}
return x;
}
var s2 = recursive_sum2(20);
console.log(s2); // 20
Here's how I would go about writing the two functions. I used the recursive sum function earlier because it's much easier to understand whats going on in relation to what you are doing (well I hope so). Hopefully this lets you compare the differences.
function recursive_sum(n) {
if (n === 1) {
return 1
} else {
return n + recursive_sum(n - 1);
}
}
function recur(arg){
if(arg < 10){
return [arg].concat(recur(arg + 1));
} else {
return [10];
}
}
console.log(recur(3)); // [3, 4, 5, 6, 7, 8, 9, 10]
There's no course here that I have seen recursion on, so I am not sure what your knowledge in the area is. If in doubt, don't hesitate to get out a piece of paper and draw a small example of how your code is working (as I did in this case).
There's plenty of python courses here, it may help to get stuck into them. The same recursive function in python is (well I think) easier to understand for learning because it's easier to see the concatenation.
def recur(arg):
if arg < 10:
return [arg] + recur(arg + 1)
else:
return [10]
print recur(3) # [3, 4, 5, 6, 7, 8, 9, 10]
brandonlind2
7,823 Pointsbrandonlind2
7,823 Pointsok thanks! :) I really appreciate the long explanation