factorial of a number

前端 未结 28 2415
滥情空心
滥情空心 2020-12-09 11:57

I have the following code but it is not giving perfect result for factorial can u find it out plz



        
28条回答
  •  攒了一身酷
    2020-12-09 12:34

    A slight edit to Anton's code:

    function fact(x) {
       if(x>0)
           return x* fact(x-1);
       if(x===0)
           return 1;
       return null;
    
    }
    

    (factorial of a negative doesn't exist, but factorial of 0 is equal to 1, in this case, if a number is smaller than 0, the function will return null)

提交回复
热议问题