Answers for "Use Recursion to Create a Countdown"

3

Basic JavaScript: Use Recursion to Create a Countdown

function countdown(n){
  if (n < 1) {
    return [];
  } else {
    const arr = countdown(n - 1);
    arr.unshift(n);
    return arr;
  }
}
console.log(countdown(5)); // [5, 4, 3, 2, 1]
Posted by: Guest on December-20-2019
0

Use Recursion to Create a Countdown

function countdown(n){
   return n < 1 ? [] : [n].concat(countdown(n - 1));
}
Posted by: Guest on October-01-2021

Code answers related to "Use Recursion to Create a Countdown"

Code answers related to "Javascript"

Browse Popular Code Answers by Language