'Double countdown using recursion function?

I'm trying to write a recursion using JavaScript, without using global variables but i am not sure that the program can be possible like desire output.

Question is "Given a non-negative integer n, create a double countdown-pattern in the output." Example: If you ran doubleCountdown(5) you would also get the following output:

5
4
3
2
1
4
3
2
1
0

I have tried following ways: In this way getting down to up output but not exact output:

double_countdown(5);
function double_countdown(n){
    if(n == 0){
        console.log(n);
        return
    }
    console.log(n);
    double_countdown(n-1);
    console.log(n);
}

In this ways, i have used global for desired output but not standard way.

let second_n = 0;
let double_count =0;
double_countdown(5);
function double_countdown(n){
    if(n == 0 && double_count == 0){
        double_count = 1;
        double_countdown(second_n-1);
        console.log(n);
    }
    if(n == 0){
        return
    }else{
        second_n +=1;
    }
    console.log(n);
    double_countdown(n-1);
}


Solution 1:[1]

I would write the single countdown as a recursive function and have a double countdown function simply call it twice. This keeps all the parts simple.

const countdown = (s, e = 0) => 
  s < e ? [] : [s, ...countdown (s -1, e)]

const doubleCountdown = (n) => 
  [...countdown (n, 1), ...countdown (n - 1, 0)]

console .log (doubleCountdown (5))

You can then log the answer however you want for your homework. Perhaps something like for (let n of doubleCountdown (5)) {console .log (n)}

Solution 2:[2]

Implement the recursive function in a way that it keeps track of its recursion state by providing and working with more parameters than just the required first parameter (which is the current count down value).

Thus one would implement e.g. a function which is capable of running the count down cycle N times. And a double_countdown respectively countDownTwice function could be derived from such a generic implementation.

function countDownNTimes(value = 0, times = 1, initialValue) {
  // initially invoked with an `undefined` value for `initialValue`.
  initialValue ??= value;

  console.log(value);

  if (times >= 2) {
    if (value <= 1) {

      // does trigger a new recursion cycle with minimized boundaries.
      countDownNTimes(initialValue - 1, --times, initialValue);
    } else {

      // stays within the current recursion of minimized boundaries.
      countDownNTimes(--value, times, initialValue);
    }
  } else if ((times >= 1) && (value >= 1))  {

    // stays within the current, final recursion of reastablished boundaries.
    countDownNTimes(--value, times, initialValue);
  }
}
const countDownTwice = value => countDownNTimes(value, 2);

console.log('... countDownTwice(5) ...');
countDownTwice(5);

console.log('... countDownNTimes(3, 3) ...');
countDownNTimes(3, 3);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Solution 3:[3]

Finally, after trying many times. I got the answer. If anything wrong then please help for more improve.

double_countdown(5);
function double_countdown(n,sc=0){
    if(sc == 0) sc = n;
    if(n == 0 ) return ;
    console.log(n);
    double_countdown(n-1,sc);
    console.log(sc-n);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Scott Sauyet
Solution 2
Solution 3 Dev D