Function is not returning the array

Hi, the program is supposed to return an array, but it returns undefined. Before returning the array, I tried to print the array independently and it seems ok. Not sure what is happening, asking for help!

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

  }
  else {
    countDownArray.push(n); 
     countdown(n-1);
  }
  }
  
}
  

You’re not returning anything, you return if n is smaller than 1 and if it’s equal to 1, but if it’s bigger than 1 it doesn’t return anything.

You are use recursion when you can countdown(n-1), let’s say n=2, then it will return countDownArray, but once it returns it’ll return to the “original” function, which will not do anything with it.

You need to add a return before countdown(n-1), otherwise, the array will just end up there and nothing will happen to it.

I’m not sure if I explained this well, but basically, the values are returning to the function, and once they go back to the original nothing is done with the array.

1 Like

Hi, Thank you very much.
Your solution solves the problem. It help me to clarify my idea on ‘return’, I had a misconception that “return” will totally shut down the function. Actually, it is giving control back to the calling function, in this case, countdown fucntion.
But, I cannot convince myself on the following issues,

  1. countDownArray seems a valid array, how? → right before attempting to return it, I can print the array using console.log() function, and it seems we are getting expected result.
  2. Out of nowhere, if I simply assigning countDownArray some random element( lets say, right before the return statement we add, countDownArray = [5,4,3,2,1]; ), it won’t return undefined, it returns the assigned value (in our hypthothetical case, [5,4,3,2,1] .
    To me, it seems like a contradictory behavior.

The return statement does both: it stops the execution of a function, and determine a value to be returned to the function caller.
Remeber that in JavaScript if a return is omitted the function will return undefined instead.

In this case you are using a recursion, meaning that you are creating a stack of function one on top of the other.
You can imagine it like a pile of document one on top of the other.
You process it one at a time.
You have to specify that you want the result of said “document” to be used in the next one, otherwise by default it will return undefined.

Let’s see this example:

var result = 0;
function sum(a, b) {
  // NOTE: this function is changing `result`.
  result = a + b; 
}

// let's assign the return of sum to a variable
var temp = sum(1,2);

// if we were to inspect what sum returned:
console.log(temp) // undefined
// but the value has been updated as expected
console.log(result) // 3

Likewise in your recursion:

// you are updating the value correctly
    countDownArray.push(n); 
// then add a new function to the pile
     countdown(n-1);
// but there's no return so I am gonna return undefined to my caller.

Hope it helps :sparkles:

1 Like

Thank you so much! I cannot thank you enough for the first example code, I believe I need to try a similar code by myself like the first example (which I will do). It will help, not just with this problem, but with many more upcoming problems. Thank you, I appreciate your time.

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
1 Like