If you want a result from a recursive function, all code paths through the function must return something. Your code isn't returning anything in the num!=1 case. It should be returning the result of calling itself, e.g. (see the *** line):
var fact=5;
function calfact(num)
{
if(num!=1)
{
fact=fact*(num-1);
num=num-1;
return calfact(num); // ***
}
else
{
return fact;
}
}
Your function is using a global variable, which isn't a great idea as it means the funtion isn't self-contained; and isn't a true factorial function, because you're effectively using two inputs (fact โ the global and num, the argument)
If you want a true factorial, you don't need a global variable, just work from the argument itself:
function factorial(num) {
if (num < 0) {
throw new Error("num must not be negative");
}
if (num <= 1) {
// Both 1! and 0! are defined as 1
return 1;
}
return num * factorial(num - 1);
}
console.log(factorial(5)); // 120
Or of course, more compactly:
function factorial(num) {
if (num < 0) {
throw new Error("num must not be negative");
}
return num <= 1 ? 1 : num * factorial(num - 1);
}
(More about 0!: https://en.wikipedia.org/wiki/Factorial)
Answer from T.J. Crowder on Stack OverflowIf you want a result from a recursive function, all code paths through the function must return something. Your code isn't returning anything in the num!=1 case. It should be returning the result of calling itself, e.g. (see the *** line):
var fact=5;
function calfact(num)
{
if(num!=1)
{
fact=fact*(num-1);
num=num-1;
return calfact(num); // ***
}
else
{
return fact;
}
}
Your function is using a global variable, which isn't a great idea as it means the funtion isn't self-contained; and isn't a true factorial function, because you're effectively using two inputs (fact โ the global and num, the argument)
If you want a true factorial, you don't need a global variable, just work from the argument itself:
function factorial(num) {
if (num < 0) {
throw new Error("num must not be negative");
}
if (num <= 1) {
// Both 1! and 0! are defined as 1
return 1;
}
return num * factorial(num - 1);
}
console.log(factorial(5)); // 120
Or of course, more compactly:
function factorial(num) {
if (num < 0) {
throw new Error("num must not be negative");
}
return num <= 1 ? 1 : num * factorial(num - 1);
}
(More about 0!: https://en.wikipedia.org/wiki/Factorial)
var fact=5;
function calfact(num){
if(num!=1){
fact=fact*(num-1);
num=num-1;
return calfact(num);//the missing thing
}else{
return fact;//why fact? i think it should be 1
}
}
By the way, your approach is maybe working, but really bad style.May do this:
function calfact(num){
if(num!=1){
return calfact(num-1)*num;
}else{
return 1;
}
}
Or short:
calfact=num=>num==1?1:calfact(num-1)*num;
Videos
You don't need recursion for that:
/**
* Calculate factorial, optionally using a difference other than 1 with previous value.
* Example: factorial(6, 2) // 6*4*2 = 48
*/
var factorial = function(n, d) {
if (!d) {d = 1;}
var product = 1;
while (n > 1) {
product *= n;
n -= d;
}
return product;
};
console.log(factorial(6, 2)); // 48
console.log(factorial(6)); // 720
Note: Declare local variables inside the function with keyword 'var'. Otherwise they become globals and the second time you attempt to use a function may produce wrong results.
Usually, writing a function for Factorial is an exercise on writing recursive function. The first example code is not recursive and just an overly complicated way of calculating a factorial by multiplying the numbers iteratively so I'll skip that.
The second code is recursive, and it is following the recursive definition of factorial in your usual mathematics:
f: N => N, f(x) = x! = { x < 1 : 1, else : x (x - 1)! }
Or equivalently in JavaScript:
let fac = n => n < 1 ? 1 : n * fac(n - 1);
An expansion of an example computation would look like:
5!
5(4!)
5(4(3!))
5(4(3(2!)))
5(4(3(2(1))))
5(4(3(2(1(0!)))))
5(4(3(2(1(1)))))
120
So I was watching a JavaScript tutorial series and the current topic was Function Expressions then I searched in google "javascript when do u assign a function to a variable" the top search result was the MDN Web Docs for JS Functions. As I was reading the documentation I came across this example function:
function factorial(n) {
if ((n === 0) || (n === 1))
return 1;
else
return (n * factorial(n - 1));
}I know what factorial is but I can't understand how the function got the answer the else part is confusing to me, this is also the first time I came across a function that calls itself.
Can somebody explain to me how the else part works? I just can't get over this simple example. Thanks!