recursion - 06

Recursion must have a base case where the base case is defined with an if statement. Base case means the loop will stop here.

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}
console.log(countup(5));


function factorialize(num){
 if(num === 0) {
   return 1;
 } else {
   return num * factorialize(num - 1)
 }
}
console.log(factorialize(5));


Post a Comment

Previous Post Next Post