The problem is that you do not pass on the array to the recursive call, so each recursive execution creates a new, empty array. As a consequence, it does not return the array that had a value pushed to it, but the new, empty one that is coming back from the recursive calls.
Secondly, you never push value 1 to the array. So it would be better to stop the recursion at 0 instead of 1.
So taking those two fixes, you get this:
function countdown(n, newArr=[]) {
if (n <= 0) {
return newArr;
}
newArr.push(n);
return countdown(n - 1, newArr)
}
console.log(countdown(5));
Your alternative solution is clean, because it does not need to pass an array as argument. It uses the returned array to add the next value to it (in front of it). It would have my preference.
To understand how it works, print out the intermediate values:
function countdown(n) {
if (n < 1) {
console.log("At the end of recursion. Creating and returning an empty array.");
return [];
} else {
const arr = countdown(n - 1);
console.log("Got the following array back from the recursive call:");
console.log(JSON.stringify(arr));
arr.unshift(n);
console.log("Prefixing it with " + n + " and returning the result:");
console.log(JSON.stringify(arr));
return arr;
}
}
var result = countdown(5);
Answer from trincot on Stack OverflowUse Recursion to Create a CountdownPassed (JS Algorithm)
Using recursion to create a countdown in Javascript
python - Recursion countdown - Stack Overflow
Basic JavaScript - Use Recursion to Create a Countdown
Videos
The problem is that you do not pass on the array to the recursive call, so each recursive execution creates a new, empty array. As a consequence, it does not return the array that had a value pushed to it, but the new, empty one that is coming back from the recursive calls.
Secondly, you never push value 1 to the array. So it would be better to stop the recursion at 0 instead of 1.
So taking those two fixes, you get this:
function countdown(n, newArr=[]) {
if (n <= 0) {
return newArr;
}
newArr.push(n);
return countdown(n - 1, newArr)
}
console.log(countdown(5));
Your alternative solution is clean, because it does not need to pass an array as argument. It uses the returned array to add the next value to it (in front of it). It would have my preference.
To understand how it works, print out the intermediate values:
function countdown(n) {
if (n < 1) {
console.log("At the end of recursion. Creating and returning an empty array.");
return [];
} else {
const arr = countdown(n - 1);
console.log("Got the following array back from the recursive call:");
console.log(JSON.stringify(arr));
arr.unshift(n);
console.log("Prefixing it with " + n + " and returning the result:");
console.log(JSON.stringify(arr));
return arr;
}
}
var result = countdown(5);
You need to hand over the result array for the recursive call. And you need to check if no value is left, ten return the result array.
function countdown(n, result = []) {
if (n < 1) return result;
result.push(n);
return countdown(n - 1, result);
}
console.log(countdown(5));
As another approach, you could return an array and for the exit condition take the final value, otherwise take n and the spreaded result of the recursive call.
function countdown(n) {
if (n < 1) return [];
return [n, ...countdown(n - 1)];
}
console.log(countdown(5));
Hi guys, I am stuck on a very simple recursion problem where I am asked to use recursion to count down from n and log each number. The prompt is as follows:
"Write a recursive function that accepts a positive integer n as an input and logs every number from n (inclusive) to 0 (exclusive) to the console"
And here is my attempt:
function countDown (n) {
//base case
if (n <= 0) { return 0}
// recursive call
else { countDown (n-1)
console.log(n)
}
Trying to figure out where I am going wrong here and I appreciate any feedback. Thanks!!!
This would work
def count_down(n):
print(n)
if n>0:
count_down(n-1)
The only flaw is that from what I have tested, it doesn't like large numbers (over around 950). This is due to too much recursion, and cannot be fixed unless the code was made without recursion, like so:
def count_down(n):
for i in range(n, 0, -1):
print(i)
This is JavaScript code:
function countdown(n){
if(n<1){
return [];
}else{
var countArray = countdown(n-1);
countArray.unshift(n);
return countArray;
}
}
console.log(countdown(10));
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1] // array of recursion
hello everyone,I'm having a hard time understanding how the code from this exercise works, i tried using debugger but I got more confused
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 ]
what I understand so far is that the function countup(5) gets executed then the recursion keeps happening till n=0
if (n < 1) {return [];}
now we have an empty array and that's all I understand😅
1.why am I not kicked out of the if statement after that, how does the function keeps executing??
2.the function then start pushing the numbers incrementally, what causes the increment here?? Also when I the debugger I noticed that the function would hit the last curly bracket and then jump back tocountArray.push(n); how does that happen .. any clarification would be appreciated
That if clause is checking whether the argument is less than 1. If you’ve passed in 5, that’s more than 1 so the if block is never executed.
This is the recursion part. The thing to think about is that the code block will recur until the if clause is met, which returns an empty array. So the first line of the else cause is what will execute recursively. This returns an empty array which then has all of the numbers pushed into it as the blocks of code from each prior recursion get executed.
I’m not a huge fan of this sort of recursive pattern because it values brevity over clarity. In most real world circumstances, the marginal reduction in computational overhead will be of no benefit if others working on the code have a hard time understanding it.
the return happens when n<1.
As far as how the recursion works, it happens on the first line of the else block. You create a variable and assign it the value of executing countup with (n-1). You can't go to the next line until this line has resolved.
So what happens? you run another instance of countup, this time with n-1. If that n is now less than 1, it'll return the empty array, and you don't hit the else block, meaning you don't execute countup again.
At that point the countup inside of your countup returns []. You do get kicked out of the if statement, but it's the inside countup, meaning there's still the outside countup, where you were figuring out what countarray is. Now that we know that countArray is [], we can push n to that array and return it.