Use Recursion to Create a Countdown HELP

Tell us what’s happening:
Please, could someone check my code, why reverse doesn’t work for me? It prints: [ 5, 3, 1, 2, 4 ]… :sob:
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.reverse();

// 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/89.0.4389.90 Safari/537.36.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

There’s a better way! Instead of reversing each recursion why not reversing the way you push?

Like with the unshift

Oh, okay, thank you! That’s cool!

But still, why reverse doesn’t work here? Even if my code is one line longer, shouldn’t it print correct answer with reverse?

you are reversing at each time
so it starts with [1], then 2 is added, [1,2], then it is reversed, [2,1], then 3 is added, [2,1,3], then it is reversed, [3,1,2], and so on


I see… Thank you for explaining this to me!

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