I need help here please , I'm stuck

Tell us what’s happening:
Describe your issue in detail here.
Describe your issue in detail here.
with the code above, I know that

const countArray = countup(n - 1); will result in
countArray = countup(4)
countup(4) = countup(3)
countup(3) = countup(2)
countup(2) = countup(1)
countup(1) = countup(0)
countup(0) =  [ ]

hence since countup(0) = [ ]

countArray = [ ]
countup(4) = [ ]
countup(3) = [ ]
countup(2) = [ ]
countup(1) = [ ]
countup(0) = [ ]

meaning const countArray = [ ]
and as a result countArray.push(n)
should result [5]
so my question is :
why did it result in [1,2,3,4,5] and how did it happened?
please I need a clear explanation because maybe how I was thought was wrong

  **Your code so far**

// Only change code below this line
function countdown(n){
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/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thanks but I’m not seeing this code

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}
console.log(countup(5))

That’s because you didn’t put that code in your post… I didn’t add any code to you post.

I went ahead and added spoiler tags to this solution you’ve found though.

ok then please add this code at the beginning of my post for me and help me if you understands what caused the output to be [1,2,3,4,5]

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}
console.log(countup(5))

output = [1,2,3,4,5]

Hi, take a look at my thread and the solution I have marked in

I had the same confusion, but the lessons to that point if I re call correctly don’t teach us about data structure yet, the solution answers that question fairly.

You can also use the help of this website : Online JavaScript Compiler, Visual Debugger, and AI Tutor - Learn JavaScript programming by visualizing code
it will show you how the function runs.

Cheers.

1 Like

thanks a lot bro
I really appreciate it

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