search

Home  >  Q&A  >  body text

javascript - What's wrong with the code that implements hierarchy


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

高洛峰高洛峰2819 days ago450

reply all(2)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-19 10:48:33

    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)

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-19 10:48:33

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

    reply
    0
  • Cancelreply