Recursion ! why doesnot this work

Okay this may sound stupid but i am a beginner and i want to know why this doesn’t work . I want to make a countdown array using recursion .


//Only change code below this line

var myArray = [ ];

function countdown(n){
  if (n < 0) {
    return myArray;
  } else {
    myArray.push(n);
    countdown(n-1);
  }
}

console.log(countdown(5)); // [5, 4, 3, 2, 1]

Please return the array where you stored values of the countdown.

return myArray

in else statement.

In your else statemente you are not returning anything, so in that case your function returns undefined. But you also need to consider this:

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

What @ilenia says is important here. One of the things you will find with recursion is that you want to define an accumulator, ie a collection that you use to accumulate values ias you recurse. Then once the terminal condition is met, the accumulator gets returned.

This is a standard pattern: you are spot on with what you’re trying to do. It just won’t work properly because of that global variable. There are two ways to deal with this: one involves no extra knowledge, one involves something you won’t have been taught yet:

First version, just put the definition of the recursive function inside another function, and call it. This is, conceptually, the same as writing a loop, so is fairly easy to grok:

function countdown(n) {
  var myArray = [];
  
  function loop(n) {
    /* the logic you already have goes here, but the function is
     * called `loop`, not `countdown` */
  }

  return loop(n);
}

The second version uses default parameters, so myArray gets initialised as [], it doesn’t need to be passed as an argument, so you can call countdown(10) and myArray will start off as []. It makes the logic cleaner, as you don’t need an inner function, but you haven’t been taught this at the minute so it’s a little bit harder to grok.

function countdown(n, myArray = []) {
  /* the logic goes here _except_ you pass myArray to
   * `countdown` each time -- `countdown(n - 1, myArray)` */
}

EDIT as examples (don’t look at this until after you get yours working): https://repl.it/@danielcouper81/Recursive-countdown-examples

1 Like

A good recursive function should not change anything’s state, such as shifting or popping from an array. Instead, you want to return and/or pass new values instead. Like using arr.slice(1) instead of arr.shift() (and that’s a pattern you’ll see a lot in recursive functions that work with arrays). When you do that, you can use the substitution model, which I explain here using factorial as the recursive function.

1 Like