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
Link to the challenge: