1

I'm pretty new to recursion and Im having trouble returning the value I want into an array. I have a simple function called countDown which needs to take in an argument of type integer in this case the parameter/argument is the letter (n). and I want to count backwards starting from the number (n) all the way to 1. so for example if I pass in the number 4 I would like to return [4, 3, 2, 1] and I need to do this recursively. I believe I have gotten close because in my code I simply put a console.log(n) and I can see now the numbers are printing out 4, 3, 2, 1 however I need to return these numbers in an array and I am pretty lost. I'm familiar with .push() but that doesn't seem to work and I have tried .concat() but I'm not able to get it to work either. Any help is much appreciated!

function countDown(n) {
  if (n < 1) {
    return [];
  } else {
    console.log(n);
    let j = countDown(n - 1);
  }
}

countDown(4);

5 Answers 5

1

You are indeed pretty close. There are going 2 things wrong in your snippet.

  1. You do not return a value if n is not smaller than 1 (the else scenario).
  2. You do log n, but don't add it to the result.

Without changing a lot, a solution might look like this:

function countDown(n) {
  if (n < 1) {
    return [];
  } else {
    // get the countdown of n - 1
    const ns = countDown(n - 1);
    // add the current n in front
    ns.unshift(n);
    // return the list
    return ns;
  }
}

console.log(countDown(4));

Sign up to request clarification or add additional context in comments.

1 Comment

@ 3limin4t0r Thank so much for helping me out! I didn't think to use unsift(n). I really like the answer you gave and I will learn from it. Cheers.
1

Make sure you return something in every case!

You're doing it for the base case (return []), but you need to return something that includes the recursive call in other cases (return // something that uses countDown(n-1)).

function countDown(n) {
  if (n < 1) return [];
  return [n, ...countDown(n-1)];
}
console.log(countDown(4));

3 Comments

Or of course, const countDown = (n) => n < 1 ? [] : [n, ... countDown (n - 1)].
@ Trevor Dixon & Scott Sauyet Thank you all for helping out! I have not yet gotten to spread ... syntax and im just now getting familiar with ES6 but this code works and I really appreciate it.
@ClaudeManbodh: When you do, the spread syntax can simplify many things! Best of luck!
0

You appear to be nearly there, but when using recursion state will need to be passed to the next iteration.

So below is an example where the arr parameter if left blank will create the initial array, and then the recursive part can just keep passing this down.

function countDown(n, arr) {
  if (!arr) arr = [];
  if (n < 1) {
    return arr;
  } else {
    arr.push(n);
    return countDown(n -1, arr);
  }
}

console.log( countDown(4) );

1 Comment

Default parameters are now widely supported. They would simply this a bit.
0

Is this does what you want?

let arr = [];

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

countDown(4);
console.log(arr);

1 Comment

This is a wonderful demonstration of the problems with global variables. If you add to the end countDown(5); console.log(arr);, you will see that it now contains [4, 3, 2, 1, 5, 4, 3, 2, 1]. Not recommended! See other answers here for alternatives.
0

Try something like this

function countDown(n, a = []) {

  if (n < 1) {
    console.log(a);
    return a;
  } else {
    a.push(n);
    let j = countDown(n - 1,a);
  }
}

countDown(4);

1 Comment

No. This does not return anything in most cases, only logging it. If you replace let j = with return, this would be reasonable. (And then the console.log call would be unnecessary.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.