Recursive without loop last basic question

How do i push first before I make my recursive call?
The problem wants me to make a recursive function without a loop .
Your code so far


// Only change code below this line
function countdown(n){
if(n < 1){
  return [];
}else{
  Const CountArray = countdown(n - 1);
}
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/88.0.4324.190 Safari/537.36.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

Two things:

  • It’s lowercase const
  • You always have to return an array. You have the base case correct when n<1 returns an empty array. Now you need to work on the other case when you make the recursive call. Again, the key here is that you have to return an array. Right now you have a return statement at the very end of the function that is returning nothing (undefined technically). You’ll want that return statement to be inside the else block, and it must return an array.

You are actually very close. Let’s pretend that you called this function initially with 5 as the passed in value. You know that the array you are going to ultimately return is going to have 5 as the first element. So you’ll need to put 5 (or in general, n) at the beginning of the array you are going to return).

I probably just helped you too much but hopefully it will become clear soon.

I did this because i thought if push does not work i could add it with unshift into the array and it worked . I guess i have to research more on the diferent uses of push and shift cos i’m not sure why push didn’t work
}else{

const countArray = countdown(n - 1);

countArray.unshift(n);

return countArray;

}

What you just posted here is correct. Are you now passing the challenge?

Array.push() didn’t work because that adds a value to the end of the array. If the instructions had asked you to count up from 1 to n then push would have been the answer. But the instructions were asking you to count down from n, so n goes on the front of the array each time, which is what unshift does.

yes I’m on the last one.
bit more weird, I’m not sure how i would handle two arguments and make recursive call on both

this is the question
We have defined a function named rangeOfNumbers with two parameters. The function should return an array of integers which begins with a number represented by the startNum parameter and ends with a number represented by the endNum parameter. The starting number will always be less than or equal to the ending number. Your function must use recursion by calling itself and not use loops of any kind. It should also work for cases where both startNum and endNum are the same.

what i tried so far
function rangeOfNumbers(startNum, endNum) {

if(startNum < 0){

return [];

}else{

const countArray = rangeOfNumbers(startNum - 1,  endNum -1);

countArray.unshift(startNum, endNum);

return countArray;

}

};

This is very similar to the previous one, but instead of counting from 1 to n you are counting from startNum to endNum.

Think about the array you want to return, it will be of the form:

[startNum, startNum+1, startNum+2, ..., endNum]

Which variable value is changing each step and which is stying the same? Hopefully this will give you some help in how to make the recursive call.

As for the base case, in the array above, when do we stop incrementing startNum? In other words, what is the relationship between startNum and endNum that causes us to stop?