can anyone please explain what is happening in the example code (especially after the ‘else’). I do not understand the code even after reading (several times) the explanation provided. In the forum I see a lot of references for how to write the correct solution code but I don’t understand the example code so I’m unable to figure out where to start with the solution. Thank you.
first think of ‘n’ as 5.
if n is less than 1 then return an array,
else (if the n is greater or equal 1) get the return value of countup(5-1) and push to that value n (which is 5).
what is the value of countup(5-1) in other words countup(4)?
‘n’ in this context is 4.
if n is less than 1 it return an array,
else (if the n is greater or equal to 1) get the value of countup(4-1) and push to that value ‘n’ ( which is 4).
a question for you.
what is the value of countup(4-1) ?
first think of ‘n’ as 5. Ok, I understand this
if n is less than 1 then return an array, yes I understand this
else (if the n is greater or equal 1) get the return value of countup(5-1) and push to that value n (which is 5). what is the return value of ‘countup(5-1)’?
what is the value of countup(5-1) in other words countup(4) ?
‘n’ in this context is 4. how does n become 4? n-1 is 4, but how does n become 4? How can n be the same as n-1?
if n is less than 1 it return an array,
else (if the n is greater or equal to 1) get the value of countup(4-1) and push to that value ‘n’ ( which is 4). Again, I don’t understand how the value of n has changed. we called the function with ‘5’ so how can it now be 4? n-1 is 4, but ‘n’ itself is still 5, right?
a question for you.
what is the value of countup(4-1) ? this would be countup(3)
Also - I think .push is used to add to the end of an array, but where are we actually creating the array to add to?
Hey @Sharad823,
We like to describe the initial if statement as a “base case”, and the else statement as the “recursive case”. Its often helpful to think of the “recursive case” as a question that can only be answered by calling the function again.
What is the result of countup(5)?
Well we can’t answer that question until we know the result of countup(4)…
What is the result of countup(4)?
Well we can’t answer that question until we know the result of countup(3)…
What is the result of countup(3)?
…
What is the result of countup(0)?
Oh… we know the answer to that question
If const countArray = countup(n -1); wouldn’t at this point, even calling the function the first time, make n =4 and therefore countArray.push(4) instead of 5?
Before we even get to .push(n) the first time through it looks like we’ve already subtracted 1 from n.
at this point the countup function is called again, as there is countup(n-1) written there
so, step 1, countup(5) is called, inside there there is const countArray = countup(n -1);, so now countup(4) is called, there is again that line, countup(3) is called, there is again that line, countup(2) is called, there is again that line, countup(1) is called, there is again that line, countup(0) is called - this time, with n < 1 being true, the line executed is return [], so now we have that countup(0) resolve as [], so for the line const countArray = countup(0), the value of countArray is []. Now that countArray has a value the next line can be executed, at this point n has value of 1, so we have countArray.push(1), and countArray becomes [1]. And that value is returned. At this point finally also countup(1) has an output. And so on.
Ok, so I’m not understanding this so easily. How I’m reading the function is like this:
countup(5) {
if (5 < 1) {
return [];
} else {
const countArray = countup(5 - 1); //because n - 1 = 4
countArray.push(4); //so now at this point, doesn't n = 4?
return countArray;
}
}
Perhaps I’m misunderstanding how the variable is changing. Because const countArray = countup(n - 1) is recalling the function itself, but countArray.push(n) is still accepting n as the start of the whole function, i.e. it is still that n = 5 at this point.
Right, so it seems that the else statement kind of acts like a ‘loop’. When countup(5) is called, n = 5. But when we reach the recursion statement the first time, it recalls itself with n - 1.
So, countArray.push(n) in this instance n equals whatever n equals when countup() is called again.
const countArray = countup(1); // so countArray is equal to [1]
countArray.push(2); // becase inside countup(2) the variable n is equal to 2
// now countArray is equal to [1, 2]
return countArray // and with this, the value returned is [1, 2]
countup(2) was called inside countup(3), so it happens the same thing, with n having value of 3
and this repeats again and again until the first function called returns a value