Error in my code in "Basic JavaScript: Use Recursion to Create a Countdown" challenge

Tell us what’s happening:
Hi there, why when I run my code I see “InternalError: too much recursion” expression
What are my mistakes? :face_with_monocle:

My code so far :point_down:


// Only change code below this line
function countdown(n){
if (n < 1) {
    return [];
} else {
  var countDown = countdown(n);
  countDown.push(n - 1);
  return counDown;
}

}
console.log(countdown(5));
// Only change code above this line



// This is a function that return CountUp using Recursion
// function countup(n) {
//   if (n < 1) {
//     return [];
//   } else {
//     var countArray = countup(n - 1);
//     countArray.push(n);
//     return countArray;

//   }
// }
// console.log(countup(5));
// This is a function that return CountUp using Recursion

Your browser information:

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

you always call the function with same argument, so the base case is never reach, and you have infinite recursion

var countDown = countdown(n) - this is going to loop forever which is definitely too much recursion :slight_smile:

This section should be:

var countArray = countdown(n-1);
countArray.unshift(n);
return countArray;
2 Likes