Naruto Uzumaki

IT Student

View Profile

Output Prediction Puzzle 1

What is the output of the following recursive function?

function sum(n) {
  if (n === 0) {
    return 0;
  }
  return n + sum(n - 1);
}

console.log(sum(4));

Output Prediction Puzzle 2

What is the output of the following recursive function?

function factorial(n) {
  if (n === 0 || n === 1) {
    return 1;
  }
  return n * factorial(n - 1);
}

console.log(factorial(5));