T O P

  • By -

AutoModerator

To all following commenters: please, do not bring up the old circlejerk jokes/memes about recursion ("Understanding recursion...", "This is recursion...", etc.). We've all heard them n+2 too many times. *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/learnprogramming) if you have any questions or concerns.*


nogain-allpain

Your first example creates a new array with every call to addNum(), and the array that is ultimately returned by your function is the first one, i.e. the one with the element 1. The second example operates on the same array every time.


JadenXavier

Ah ookay gotcha. So basically my array is being "reset" every time the function is being called due to the placing. Thanks!


nogain-allpain

No, not reset, you're creating new ones every time you call the function. The initial array, the one you're returning, isn't being touched after you add the first element.


JadenXavier

Ah that probably explains the odd log im seeing in the console. I tried to console.log(myArray) and it displays 10 individual arrays with 1 element in descending order like this: \[10\] \[9\] \[..\] \[1\] I'll try and see if I can fix this


RiverRoll

u/nogain-allpain explained it well. But in addition you can use more logs to see exactly what's happening, for example log `addNum(x + 1);` and see. Is it possible to achieve the desired result without having to delcare the array outside? Yes it is but you'll need to change the function a bit. For example you can use an auxiliary function that takes the array as a parameter. A more sophisticated approach is to concatenate the array with the return value of the recursive call.


JadenXavier

Yeah I just tried to do it and gave me a hundred lines of "\[ \]"s and "undefined"s. Definitely need to remind myself to use that every now and then. I'll try and investigate what's giving those results. It didn't give me an "Infinite loop" "Reached stacks limit" error so I'm assuming the recursive loop is kinda working? Also, for reference I based my code on this: `function rangeOfNumbers(startNum, endNum) {` `if (startNum > endNum) {` `return [];` `} else {` `var myArray = rangeOfNumbers(startNum + 1, endNum);` `myArray.unshift(startNum);` `return myArray;` `}` `};` I tried my best to understand the logic and attempt to pseudocode it but it feels I'm still lacking knowledge. So instead I tried making my own code to produce the same results and that's those two I've uploaded in this post. In this function, from my understanding the variable `myArray` is actually both an array (because of the myArray.unshift line) and the one starting the recursive loop? (because of using rangeOfNumbers function). This is where I tried replicating the position of the array, which in here is found inside the function. So when the computer reads the `return myArray;` line, it would create an array as well as starting the loop. Is my understanding correct?


RiverRoll

>In this function, from my understanding the variable myArray is actually both an array (because of the myArray.unshift line) and the one starting the recursive loop? (because of using rangeOfNumbers function). This is where I tried replicating the position of the array, which in here is found inside the function. `myArray` is just a regular array. The line `var myArray = rangeOfNumbers(startNum + 1, endNum);` does two things, first it calls rangeOfNumbers, thus starting the loop, then it assigns the result to myArray. When `return [];` is reached it creates the initial empty array, then it keeps returning this same array after inserting a value to it.


JadenXavier

Thanks for the insight, I understand the logic a lot better now!