Stuck on this exercise: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown

Tell us what’s happening:
I am trying to understand the process/ logic of this exercise.
it asks that if (n<1) it should return an empty array,

if (n===5) then the array shoud return [5,4,3,2,1] &
if (n===10) then the array shoud return [10,9,8…1]

just not sure enough how to put it into code.

any help much appreciated.
thank you

Your code so far


// Only change code below this line
function countdown(n){
if (n < 1){
return[];
}else {
if(n===10){
  return[n-1];
}
const countArray = countdown(n-1);
countArray.push(n);
return countArray;
}
}

// Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

The code you have now is implementing a counting up, not counting down, which is why the tests are not passing.

You have to inform the function to begin at the number it was given and then count down from there. This can be done in a few ways: using concat or the spread operator are two. Here is one example:

const countArray = [n, ...countdown(n - 1)];

Here is more info on the spread operator. In this case, we are using it on the number backwards to countdown.

Thanks :+1:

Im struggling to get my head arround the logic, you say I am using a countup, how is this?
Thats why I am not understanding how the logic quite works yet.

I cant rememeber seeing the (…countdown) or similar in the tutorials.

how are you with understanding recursion?

maybe take a look at this post:

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

The intent in this exercise is for you to take the countup example code and modify it to instead countdown. Countup uses push, but you can change that to a countdown by changing the push to a [???].

1 Like

well I used .pop, but i am stuck with:

countdown(10)

should return

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
countdown(5)

should return

[5, 4, 3, 2, 1]

using the same formula as in the example.

looking things up, maybe I need to use unshift.

1 Like

the answer was unshift, looking at examples in w3schools, unshift adds element to the array. :smiley: :smiley: :+1: :+1: :clap: :clap: :clap:

1 Like

There you go. Push adds to one side of the array and unshift adds to the other.

The explanation can some times seem daunting, and confusing, I feel that I have to break down the challenge into pieces to make sense of it sometimes.

Recursion is an especially tricky idea that requires synthesis of many previous ideas. Breaking the problem down into smaller pieces is a key technique that I use professionally every day.

we havent yet seen the keyword ‘const’ and where did ‘countArray’ come from?
many thanks

Wow!!!
can you recommend any books on JS for me to understand JS logic/ programming?

Const means something like constant
They are a bit like var but cannot be reasigned
we learn about them in ES6
the countArray is just the name of the array

many thanks,
I guessed that ‘const’ was something like that, so why cant we use ‘var’ as that is something we have already learnt, lol

Probably because of it’s scope and that var can cause problems with same named array’s

Many thanks.
Oh No!!!
the next challenge is even more complicated!!
I think it has something to do with(min,max)
I will have to break it down into pieces.

Well i do have a very long explanation for that one but, it does contain spoilers XD
Anyway good luck
and happy coding