Hi, my Name is Bruno, i’m from Brazil and my english is not that good, but here it goes:
function countup(n) {
if (n < 1) {
return [];
} else {
***const countArray = countup(n - 1);***
countArray.push(n);
return countArray;
}
}
console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]
Im studing recursion and having a lot of doubts like: why the const countArray is receving countup(n - 1)? I don’t understand, I mean, the concept of recursion i know, but this code makes it all confused. If someone here could give some help i’d aprecciate it.
Hi Bruno,
welcome to the fcc forum!
I found this counterintuitive as well. I thought the best way to answer this might be a video. So I created one for you, stepping through it in the dev tools …
(This is my first “tutorial” recording and my English isn’t all that great either, so have mercy )
Hope it helps.
2 Likes
Wow, you don’t know how helpful this video was for me. I was looking through the forums because I was confused about the array part and this just resolved my confusion. Thank you so much for making this! I am going to save your video to my javascript study list on youtube it is that great!
1 Like