简体   繁体   中英

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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