简体   繁体   English

如何在 JavaScript 中组合两个不同的函数来显示零(0)和 n 之间的 2 的倍数?

[英]How can I combine two different functions in JavaScript to display the multiples of 2 between zero(0) and n?

My goal is to combine two different functions in order to output the multiples of 2 between zero and a parameter n (ie 5 in this case).我的目标是结合两个不同的函数,以便输出 0 和参数 n 之间的 2 的倍数(在这种情况下为 5)。 Even though my program is not giving me any bugs, it is not outputting the result either.即使我的程序没有给我任何错误,它也没有输出结果。 How can I fix the error?我该如何修复错误? Thanks谢谢

 function testFunction(testOk, executethis) { if (!testOk) executethis(); } function mainFunctin(n) { for (let i = 0; i < n; i++) { return i => { testFunction(i%2 ==1, ()=>{ console.log(i, "is even"); }) } } } mainFunctin(5);

You don't need return in the loop.您不需要在循环中return

function testFunction(testOk, executethis) {
  if (!testOk) executethis();
}


function mainFunctin(n) {
  for (let i = 0; i < n; i++) {
    testFunction(i%2 ==1, ()=>{
      console.log(i, "is even");
    })
  }
}

mainFunctin(5);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM