Evgeniy
Evgeniy

Reputation: 1

Decrement in recursion. Javascript

const factorial = (n) => {
  if (n === 0) return 1;
  return factorial(n - 1) * n;
};
console.log(factorial(5)); //Output: 120


const factorial = (n) => {
  if (n === 0) return 1;
  return factorial(--n) * n;
};
console.log(factorial(5)); //Output: 0

What the difference? Why in the second example i get 0.

Upvotes: 0

Views: 27

Answers (0)

Related Questions