Where is mistake? - recursion troubles!

Hi guys!.
I have a question why this code does not work correctly?

I made a function, but for some reason it ends up displaying undefined. If you write console.log (mas) (look in the comment in the code), it will display the correct answer.

What is my mistake???

  my mysterious code


var mas=[];
function countdown(n){
if(n<1){
  // console.log(mas);
  return mas;
  }
else{
  mas.push(n);
  countdown(n-1);
}
}
console.log(countdown(10));

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

there are two major flaws with your code, i see at first glance. First, you dont declare the array you work with locally(within the function). Instead you declare it outside of it, it retains its value on every function call. if you call the function twice, the second time the “mas” array will initialize with the value it got from the first call.
The second issue your code has is, your function return a value only when it meets the if condition. In any other occasion(if you call the function with n larger or equal 1) it will return undefined, yet the challenge suggests we always return a value.

3 Likes

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

thanks for the answer!
But what I need is exactly changing the global variable every time. That is, I want to make from mas = [ ] → mas = [5,4,3,2,1]. I tried to wite this using recursion, but the code doesn’t work for some reason.
I WANT TO KNOW WHY IT DOESN’T WORK, BECAUSE THE LOGIC IS CORRECT. (I think so, I could be wrong)

You wrote what I call ‘faux recursion’. When you use a global variable, that global variable keeps accumulating the junk from each function call, which means that each function call after the first is complete gibberish.

let mas = [];
function countdown(n) {
  if (n < 1) {
    // console.log(mas);
    return mas;
  } else {
    mas.push(n);
    countdown(n - 1);
  }
}

// First time trying to use the function
console.log(countdown(3));
// Note that you return nothing, so the return value is undefined
console.log(mas);
// But you stuck a bunch of values in here

// Second time trying to use the function
console.log(countdown(5));
// Return value is still undefined
console.log(mas)
// And this has extra junk in the global variable

You need to actually create and use the return value for countdown.

2 Likes

You don’t want to use a global variable. You want to use recursion.
The logic is not correct, because your function would only work the way you expect it to once.
If you call countdown(3) you’ll set mas to [3,2,1], but then if you call countdown(5) you’ll set mas to [3,2,1,5,4,3,2,1] because you are still using the same variable.

Using a global variable is definitely the wrong way to do this. You need to use the returned value of the recursively called function.

3 Likes

thanks!
I finally understood what you were talking about. More examples and I immediately understood everything. I’ll try to rewrite the code tomorrow

1 Like

I’m glad we could help. Happy coding!

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