Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        2年前关闭。
                                                                                            
                
        
为了理解这个范围练习,我遇到了这个site's exercise。在function doMultiplication中如何精确调用function writeIt?每当我试图弄清楚这个看似简单的练习时,我都会cross目结舌。您能否分解一下以解释这一工作原理?提前致谢。



function writeIt(numTimes, multiple) {
  function doMultiplication(val, multiple) {
    i = val * multiple;
    return i;
  }

  for (let i = 0; i < numTimes; i++) {
    document.write(doMultiplication(i, multiple) + ' ');
  }
}

writeIt(5, 2);





编辑-这是我对正在发生的事情的了解,因此可以澄清我在这里不了解的内容...

因此,使用writeIt(5,2)函数,这意味着将2与val相乘?但是这里的val是多少?

我得到了for方程,numTimes是5是多少...我认为我被val部分抛弃了吗?

最佳答案

呼叫writeIt(5,2);时,会发生以下情况-

writeIt(5,2)这里numTimes = 5&multiple = 2。

现在根据函数声明,它进入for循环-

for (let i = 0; i < numTimes; i++) {
    document.write(doMultiplication(i, multiple) + ' ');
}


至 -

for (let i = 0; i < 5; i++) {
    //document.write(doMultiplication(0, 2) + ' ');
    //document.write(doMultiplication(1, 2) + ' ');
    //document.write(doMultiplication(2, 2) + ' ');
    //document.write(doMultiplication(3, 2) + ' ');
    //document.write(doMultiplication(4, 2) + ' ');
}


现在我们转到-

function doMultiplication(val, multiple) {
    i = val * multiple;
    return i;
}


从for循环中调用-

    //document.write(doMultiplication(0, 2) + ' ');


所以doMultiplication(0,2)=> 0 * 2 =>返回0; => 0 +'';

    //document.write(doMultiplication(1, 2) + ' ');


所以doMultiplication(1,2)=> 1 * 2 => return 2; => 2 +'';

    //document.write(doMultiplication(2, 2) + ' ');


所以doMultiplication(2,2)=> 2 * 2 =>返回4; => 4 +'';

    //document.write(doMultiplication(3, 2) + ' ');


所以doMultiplication(3,2)=> 3 * 2 =>返回6; => 6 +'';

    //document.write(doMultiplication(4, 2) + ' ');


所以doMultiplication(4,2)=> 4 * 2 =>返回8; => 8 +'';

10-06 08:50