javascript - What's wrong with the code that implements hierarchy
高洛峰
高洛峰 2017-05-19 10:46:33
0
2
443


Why doesn’t this way of writing work? What's wrong?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
我想大声告诉你

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)

过去多啦不再A梦

//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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template