factorial of a number

前端 未结 28 2497
滥情空心
滥情空心 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:55

    i am quite new to javascript and would be happy to know any improvements that could be made to this answer

    var a = 1;
    function factorial(num) {
        if (num == 0) {
            return 1;
        } else if (num < 0) {
            return undefined;
        } else {
        for(i = num; i > 0; i--){
            a *= i;
        }
        return a;
        }
    }
    var b = factorial(5);
    console.log(b);

提交回复
热议问题