js函数计算任意数字的阶乘

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" id="number" placeholder="请输入数字"><br>
    <button id="btn">=</button><br>
    <input type="text" id="result" placeholder="它的阶乘是">
    <script>
        //1.获取元素值
        btn.onclick=function(){
            var n=number.value-0;
            sum=1
           //计算数字阶乘
           for(var i=n;i>=1;i--){
               sum*=i;
           }
           result.value=sum;
        }
    </script>
</body>
</html>
posted @ 2020-11-04 12:27  无敌臭弟弟  阅读(667)  评论(0编辑  收藏  举报