js帮看下这个简单的求阶乘和代码为啥死循环

JavaScript012

js帮看下这个简单的求阶乘和代码为啥死循环,第1张

你的Javascript程序中for循环的循环变量i都没有定义,应该在相应的函数中定义,或者在for循环中定义(前面加var)就行了,你的Javascript程序我帮你改完了,你看看吧(改动的地方见注释)

<script type=text/javascript>

console.log(getFactorialSum(4))

function getFactorial(n){ 

 var factorial=1

 for(var i=ni>0i--){ //这里定义循环变量i,前面加var把for(i=ni>0i--)改成for(var i=ni>0i--)

  factorial*=i

 }

 return factorial

}

function getFactorialSum(n){ 

 var sum=0

 for(var i=0i<ni++){ //这里定义循环变量i,前面加var把for(i=0i<ni++)改成for(var i=0i<ni++)

  sum+=getFactorial(n-i)

 }

 return sum

}

</script>

运行结果

33

LZ配置挺合理的,硬盘不推荐11代的,买希捷 500GB 7200.12 16M 实际速度快于11代

主板基本620块能拿下

显卡基本750块左右,不过这卡比较热,得做好没货的准备

JS帮装机是不收钱的

<div>

<input type="text" name="num1" id="num1"/>

<select name="myoperator" id="myoperator">

<option value="+" selected="selected">+</option>

<option value="-">-</option>

<option value="*">*</option>

<option value="/">/</option>

</select>

<input type="text" name="num2" id="num2"/>

<input type="button" name="calc" value="等于" onclick="oper()"/>

<input type="text" name="myresult" id="myresult"/>

</div>

<script type="text/javascript">

function oper()

{

var n1 = parseInt(document.getElementById("num1").value)

var n2 = parseInt(document.getElementById("num2").value)

//alert(n1)

var otype = document.getElementById("myoperator").value

if(null == otype || "" == otype)

{

alert("ERROR")

return

}

var res = document.getElementById("myresult")

if("+" == otype)

{

res.value = n1+n2

}

else if("-" == otype)

{

res.value = n1-n2

}

else if("*" == otype)

{

res.value = n1*n2

}

else if("/" == otype)

{

res.value = n1/n2

}

}

</script>

本地测试已经通过,这是一个简单实现,没有加验证(判断非数字和字母为0等异常),希望对你有用。