Hi everyone, this is my first post, happy to be here at freecodecamp, awesome site!
After doing some forum searching, I am slowing wrapping my head around recursion, but solution 4 is the closest thing to coding black magic since 0x5F3759DF.
To recap, the solution is:
return n < 1 ?  : [n, ...countdown(n - 1)];
Could someone please explain this, especially the effect of the ellipses?
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.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (
</>) will also add backticks around text.
Note: Backticks are not single quotes.
Are you familiar with ternaries?
n < 1 ?  : [n, ... countdown(n - 1)]
is a ternary expression that will evaluate to
n < 1 or
[n, ...countdown(n - 1)] otherwise.
[n, ...countdown(n - 1)]
builds an array that starts with
n and contains the
spread contents of
countdown(n - 1).
The rest of the logic is the same as the examples with the
if statement and multiple
In terms of wrapping your head around recurrsion, the topic was discussed at length here: Replace Loops using Recursion -1 explanation
I hope these links help somewhat. If not, feel free to ask for clarification on anything.
I am as yet unfamiliar with ternaries, but will investigate further, along with spread contents (I hope that is what I google).
EDIT: Actually the ternary was fine, I just didn’t recall ‘?’ being called that
Thanks for the speedy reply.
Thanks for the syntax correction for code.
The linked articles explained each issue perfectly.