First of all, don’t send screenshots, I can’t modify your code even if I want. Secondly, onclick is a callback function. When class=‘jisuan’ triggers the onclick event, the parameter n will not be passed to you, and your way of writing will never arrive
answer.innerHTML = factorial(n)
This statement. can be changed to this:
jisuan.onclick = function() {
var n = document.getElementById('jieceng').value
function factorial(n) {
if( n > 1){
//你的代码
}else {
//你的代码
}
}
answer.innerHTML = factorial(n)
}
I will never write code for anyone who sends screenshots again (escape)
//Update: @clearwell's answer is the correct answer, my answer was not well thought out.
1.return will terminate the execution of the function, so the statement adding content to span will never be executed.
2. The parameter of the factorial function is n, but you get the value of n again, which means that no matter how many parameters you pass, n will always be the value of input in the end. Therefore, the line (line 25) that reassigns n can be removed, and then the parameters passed each time are 1 less than the last time.
First of all, don’t send screenshots, I can’t modify your code even if I want.
Secondly, onclick is a callback function. When class=‘jisuan’ triggers the onclick event, the parameter n will not be passed to you, and your way of writing will never arrive
This statement.
can be changed to this:
I will never write code for anyone who sends screenshots again (escape)
//Update: @clearwell's answer is the correct answer, my answer was not well thought out.
1.return will terminate the execution of the function, so the statement adding content to span will never be executed.
2. The parameter of the factorial function is n, but you get the value of n again, which means that no matter how many parameters you pass, n will always be the value of input in the end. Therefore, the line (line 25) that reassigns n can be removed, and then the parameters passed each time are 1 less than the last time.