大神们 ,才学了两天js的菜鸟求问只用双层for循环求怎么做?
7 回答
蜂之谷
TA贡献564条经验 获得超863个赞
<script type="text/javascript"> var sum = 0; for(var i=1;i<=20;i++){ var jie = 1; for(var j=1;j<=i;j++){ jie *= j; } alert("第"+i+"个阶乘:"+jie) sum += jie; } alert("和:"+sum) </script>
慕的地5204680
TA贡献8条经验 获得超13个赞
//双层for循环 var dataArray = [1,2,3]; var sum = 0; for (var i=0; i<dataArray.length; i++) { var result = 1; for(var j=1; j<=dataArray[i]; j++) { result *= j; } sum += result; } console.log(sum); //利用arguments的callee方法,array的map和reduce方法 var caculateFactorial = function(x) { if (x===0) { return 1; } else { return x* arguments.callee(x-1); } } var sumArrayFactorial = function(dataList){ return dataList.map(caculateFactorial).reduce((a, b) => a+b); } var dataList = [1,2,3,4,5]; sumArrayFactorial(dataList);
添加回答
举报
0/150
提交
取消