Answer the question
In order to leave comments, you need to log in
Factorial recursion on a simple example?
<script>
alert(fact(5));
function fact(num){
return (num < 2) ? true :
num * fact(num - 1);
} </script>
Answer the question
In order to leave comments, you need to log in
fact(5)
num = 5
5 * fact(5 - 1)
num = 5 - 1 = 4
4 * fact(4 - 1)
num = 4 - 1 = 3
3 * fact(3 - 1)
num = 3 - 1 = 2
2 * fact(2 - 1)
num = 2 - 1 = 1 (< 2)
return true
5 * 4 * 3 * 2 * true = 120, т.к. true в данном случае приравнивается к 1
num
don't act likenum *= fact(num - 1)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question