factorial of a number

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

    Here's a short recursive version:

    function doFact(n) {
      return +!(+(n)) || doFact(n - 1) * n;
    }
    
    function factorialFromInput() {
      var theInputVal = document.getElementsByTagName("input")[0].value;
      var theContainer = document.getElementById("resultContainer");
      theContainer.innerHTML = "" + doFact(Math.abs(theInputVal));
    }
    .wrapper>* {
      line-height: 2em;
      width: 30%;
    }
    #resultContainer {
      border: outset grey;
      min-height: 1.1em;
      padding-left: 0.3em;
      background-color: #eff0f1;
      overflow: scroll;
    }


提交回复
热议问题