累加器无法正常工作-Javascript中的Reducer函数

问题描述

我不了解此简单的reduce函数中的累加器如何工作。我了解累加器是,在每次迭代之后,累加器值将被传递到下一个迭代,我期待这样的事情... 如果我在这里缺少什么,请告诉我。

  • Expected output,据我了解,归约功能
hellois odd // should this pass to next iteration as acc value
hellois odd  is even // should this pass to next iteration as acc value
hellois odd  is even is odd // should this pass to next iteration as acc value
hellois odd  is even is odd is even // should this pass to next iteration as acc value


  • Reduce function on array17
const array17 = [1,2,3,4];

function Evenodd(acc,element) {
  if (element % 2 == 0) return `${acc}is even \n`;
  else if (element % 3 == 0 && element % 2 != 0) {
    return `${acc}is odd \n`;
  } else if (element == 1) {
    return `${acc}is odd \n`;
  }
}

console.log(array17.reduce(Evenodd,"hello"));


-Actual output in console

hellois odd 
is even
is odd
is even

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)