# Recursion output to Array

I was working on FreeCodeCamp’s ‘Javascript Basics’ course and came to the challenge below. I attempted to find a solution using only what had been explicitly taught in the lesson thus far. The code below is what I was able to come up with (experimenting with the && operator I must admit). Actually I wrote the code without the use of the ternary operator first, and then decided to experiment farther. I wrote a console.log line running the function with the argument 10, as you can see; The console writes [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ].

So, I figured I’d figured out a way to answer the challenge with only prior knowledge from this course, but the answer is not accepted. I checked the solutions listed under “Get a hint,” but I did not like that the solutions presented all used functions that were not previously addressed in the course (.concat & const) (although I did very much appreciate the write up that was posted in May about Recursion).

The only difference in the result is the space before the the 0th element of the array, and the space after the last number of the array. Is there anything wrong with this solution? Or is it simply a different result than the challenge was written to accept? Why did the code I wrote add the spaces where it did? Would either the expected answer or my answer still be considered an array, or is there a ‘best practice’ that states those spaces should or should not be there?

Writing these questions, and being completely new to coding- I am feeling a lot like a little child that doesn’t understand the simplest things: even whether the question is worth asking in the first place! In light of that, I apologize for my nonsense, but any help will be appreciated. Thank You.

``````  **My code so far**
``````
``````
var cdArray = [];
function countdown(n) {
return n < 1 ? [] : cdArray.push(n) && countdown(n-1) && cdArray;
}
console.log(countdown(10));

``````
``````  **My browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36`

Challenge: Use Recursion to Create a Countdown

Because `cdArray` is defined as a global variable, outside of the function, the `cdArray` array, which is later returned as a result, may not contain just the entries that were added during a single count down.

In this case specifically, the unexpected entries in the array are caused by the `countdown` call below the function.

3 Likes

Thanks! That does make a lot of sense since it definitely was returning the correct values. Should I put the declaration of the Array in the recurring function?

Rather than starting with the compact ternary expression, I would look back at where recursion was introduced:

I would use this as a template for your recursive function

``````function multiply(arr, n) {
if (n <= 0) {
return 1;
} else {
return multiply(arr, n - 1) * arr[n - 1];
}
}
``````

``````function countdown(n) {
if (Base Case) {
// Code for Base Case here
} else {
// Code for Recursive Case here
}
}
``````

Recursion is a hard topic that has a way of highlighting any misunderstandings you have. Don’t feel bad about it being difficult for you - it’s difficult for everyone!

Appreciate! Yes, I had it in that form before I decided to reflect the same function with the ternary operator just to see if I could do it.
Also appreciate your reassurance. I plan on digging deep and getting up to speed on a lot of developmental principles.
Thanks again,
Daniel

The ternary version requires some trickery that ultimately I think makes it harder to read and maintain the code - it certainly is a worthy exercise to practice your skills, but I definitely think that chasing the recursion is more important than chasing the ternary in this challenge.

1 Like

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