Use Recursion to Create a Countdown hello please help me

Tell us what’s happening:
Describe your issue in detail here.
can you tell me how does .push in the example and unshift in the challlenge work? Thank you

  **Your code so far**
// Only change code below this line
function countdown(n){
if  (n < 1) {
  return [];
} else {
  const countArray = countdown(n - 1);
  return countArray;
// Only change code above this line

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

push adds an item to the end of an array.

const fruits = ['apple', 'banana'];
console.log(fruits); // ['apple', 'banana', 'pear']

unshift adds an item to the beginning of an array.

const fruits = ['apple', 'banana'];
console.log(fruits); // ['pear', 'apple', 'banana']
1 Like

unshift is adding n at the beginning of the countArray array, which is the array returned by countdown(n-1) (which is an array containing the numbers from n-1 to 1, or an empty array)

1 Like

What do you mean by ‘exactly what happens’? All that push does is add an element to the end of an array. That’s it.

Have you tried using push instead of unshift and running the code?

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.