js求5的阶乘

方案一:利用while循环
function factorial(num){  
        var result = 1;  
        while(num){  
            result *= num;  
            num--;  
        }  
        return result;  
    }  
方案二:利用函数递归
function factorial(num){  
        if(num <= 0){  
            return 1;  
        }else{  
            return num*arguments.callee(num-1);  
        }  
    }  

猜你喜欢

转载自blog.csdn.net/weixin_40292626/article/details/80542450