Hi, i have big trouble understandig this exercise.
I have this solution (which pass the test perfectly):
function countdown(n){
if (n < 1) {
return [];
} else {
const countArray = countdown(n - 1);
countArray.unshift(n);
return countArray;
}
}
console.log(countdown(3));
And my first question is, how does the code understand that “countArray” is an array if i didnt defined it?
Then, I want to know how it works… So, I will supose i use n=3
function countdown(n){
if (n < 1) {
return [];
} else {
const countArray = countdown(n - 1); **// for n=3 ==> countArray = countdown(2)**
countArray.unshift(n); **//applying the unshift ==> what happen??????, i know that it add "3" to de first space of countArray, but count array was previusly "countdown(2)... so i really dont understand what happen**
return countArray; **// now here... if i didnt understand the previus step of course i dont know what will return....**
}
}
console.log(countdown(3)); // returns [3, 2, 1]