Hej!
I’m really having problems wrapping my head around recursion as i have to spend several hours to understand the core concept and is unable to come up with recursion solutions when requested, which is challanging.
My questions is related to the countDown challange (https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown) , where one of the solutions is as follows;
function countdown(n){
return n < 1 ? : [n].concat(countdown(n - 1));
}
Could someone help me understand better how this is working?
When i enter it into JavaScript Tutor - Visualize JavaScript code execution to learn JavaScript online
i only see that it works its way down and evaluating n for each call (n =5, n=4,n=3…) and once it hits n = 0 it return an empty array.
In my head this means that this is feed into the call of countdown(1):
(countdown (0) {
return )
→ countdown(1){
return [1].concat[countdown(0)]) ( == return [1].concat() )
although when i try to do this syntax in the terminal it gives me an error which leads me to believe that i dont quite comprehend what is happening, and how.
all help and input on how to further get better when it comes to recursion is highly appreciated. I’ve searched and watched prettty much all youtube videos on the subject allthough they usually talk about the same type of thing (fibonacci, countdown, search-boxes) and go through the code , although it always feels like there is some core fundamentals missing.
Essentially same with the challange afterwards (range of numbers : freeCodeCamp Challenge Guide: Use Recursion to Create a Range of Numbers) where the #2 solution goes as:
function rangeOfNumbers(startNum, endNum) {
return startNum === endNum
? [startNum]
: rangeOfNumbers(startNum, endNum - 1).concat(endNum);
}
Absolutley mind boogled how the calling and everything works for this solution.