如何解决累加器无法正常工作-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 举报,一经查实,本站将立刻删除。