Tell us what’s happening:
I am not able to understand how the following function is creating an array. The only thing I am able to understand is that the base case is returning an empty array.
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 ]
Your code so far
// Only change code below this line
function countdown(n){
return;
}
// Only change code above this line
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36
.
Challenge: Use Recursion to Create a Countdown
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
Do any of these conversations help:
**I don’t understand the running of this code i expected that the array value will decrease from n to 1 but it’s the opposite can someone explain it to me please ? **
Your code so far
//Only change code below this line
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 ]
Challenge: Use Recursion to Create a Countdown
Link to the challenge:
https://www.fre…
I haven’t had my coffee yet, so I’m going to take shortcut and refer you to my explanation from yesterday:
Let me know if that doesn’t make sense and I can walk through the call stack of your function.