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);
  countArray.unshift(n);
  return countArray;
}
}
// Only change code above this line
console.log(countdown(10));


  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 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'];
fruits.push('pear');
console.log(fruits); // ['apple', 'banana', 'pear']

unshift adds an item to the beginning of an array.

const fruits = ['apple', 'banana'];
fruits.unshift('pear');
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.