Skip to content

Latest commit

 

History

History
30 lines (23 loc) · 524 Bytes

recursiveLooping.md

File metadata and controls

30 lines (23 loc) · 524 Bytes

Recursive looping

Here's an example of computing a factorial using a for loop in JavaScript:

function computeFactorial(num){
  var result = 1;

  for(var i = 2; i <= num; i++){
    console.log(`result of ${result} * ${i} is ${result * i}`)
    result *= i;
  }
}

computeFactorial(num);

Recursive loop:

function computeFactorialRecursively(num){
  if(num === 1){
    return 1;
  } else {
    return num * computeFactorialrecursively(num - 1);
  }
}

computeFactorialRecursively(5);