factorial of a number

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

    1. Your function doesn't return anything, ever.
    2. What do you do when x is 0?
    3. Minor point - apart from alert, you don't really do anything with the returned value.

    Try this instead, if you will (hover over the text):

    if(x==0) return 1;
    return x * fact(x-1);

    Working example: http://jsbin.com/apuka3/2

提交回复
热议问题