I can't solve this problem with only what I've learnt so far (I haven't learned about 'const' yet)

Tell us what’s happening:
I came up with a solution for this exercise that uses what I have learned in the course. But it’s rejected. I’m assuming because the array I created is outside the function, it doesn’t get created when I call on the function only. And I can’t create an array inside the function or it gets overwritten every time I call the function (and it has to be recursive, so that doesn’t work).

The solution showed on the forum, like the example on the instructions, uses this kind of “const” variable that I don’t know or understand. It hasn’t been explained in previous lessons, and it doesn’t work the same way a “var” variable works, I have tried switching it.

Yes, I can copy the solution, but I don’t understand how it works, without knowing anything about “const” variables (and I am assuming it’s a kind of variable, I’ve read a bit about them online but I have no idea really).

Your code so far


// Only change code below this line
var countArray = [];

function countdown(n){
if (n < 1) {
  return [];
} else {
  countArray.push(n);
  countdown(n-1);
}

return countArray;
}
// Only change code above this line

console.log(countdown(10))

Your browser information:

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

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

1 Like

It’s tempting to use ‘faux recursion’ and rely upon global variables, but as a general rule you want to rarely use global variables. I put a little bit about global variables below.

Let’s look at the example: (I changed const to var to show that in this case either work)

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    var countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}
console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]

The biggest difference between this code and your code is that in the example, the output from countup is captured and used.

    var countArray = countup(n - 1);
    countArray.push(n);
    return countArray;

vs

  countArray.push(n);
  countdown(n-1);

How can you modify your code to actually use the return value from countdown?


About Global Variables:

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
3 Likes

I agree 100% with what Jeremy says.

I would just add that another solution would be to not have an array variable at all but just to pass it in to the function. You could have it default to an empty array in the parameter list. I’ve had problems where that was the best solution. I’m not saying it’s better here, just that it’s a different approach.

Don’t get frustrated. JS is hard. And recursion is a hard topic in any language - until you wrap your head around it. Then it’s just weird.

1 Like

Thank you so much, Jeremy and Kevin!

This is so helpful, am starting to understand.
This was my first time posting in the forums, I am so happy I got such quick and useful help!!

Solved:

function countdown(n){
  if (n < 1) {
    return [];
  } else {
    var countArray = countdown(n - 1);
    countArray.unshift(n);
    countdown(n-1);
  }

  return countArray;
}
1 Like

Cool, good job. Good luck on the next one.

1 Like