简体   繁体   English

减少 function 执行 - 谁能帮我识别错误,因为我无法得到结果

[英]Reduce function execution - Can anyone help me in identifying the error as I am unable get the result

 const sum = (arr) => { arr.reduce((acc, val) => { return acc = acc + val; }, 0); }; console.log(sum([2, 3, 4, 5]));

You have to return the result of the call to reduce in your sum method.您必须在sum方法中返回调用结果以reduce

Also, in the reduce method you are returning the result of assigning a value.此外,在reduce方法中,您将返回分配值的结果。 Returning the result of an assignment can be confusing and there is no need for that in this case.返回分配的结果可能会令人困惑,在这种情况下不需要这样做。

Having all that in mind, your code should look:考虑到所有这些,您的代码应如下所示:

 const sum = arr => arr.reduce((acc, val) => acc + val, 0); console.log(sum([2, 3, 4, 5]));

暂无
暂无

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

相关问题 没有从 api 获取数据到 web 页面,任何人都可以帮助我解决这个错误,我该怎么做才能获取数据 - Not getting data from api into the web page, can anyone help me through this where I am getting an error and what can i do to get data 谁能帮我解释一下“ get:function()”和.prototype? - Can anyone help explain “get: function()” and .prototype to me? 谁能帮我告诉我为什么我在函数中得到一个空值 - Can anyone help and tell me why do i get a null value in the function 我正在制作导航栏,但我没有发现响应此导航栏的错误。 谁能帮我 - I am making nav bar but I don't find the error in making responsive to this nav bar. Can anyone help me 我只是想将函数 func() 中的一些 javascript 值传递给标签“p”。但它没有发生。谁能帮我解决这个问题? - I am just trying to pass some javascript value from function func() to the tag "p".But it's not happening.Can anyone help me with this? 谁能让我知道为什么会收到此错误? - Can anyone let me know why am I getting this error? 谁能帮我破译此功能? - Can anyone help me decipher this function? 谁能帮我解决这个反应错误? - can anyone help me with this react error? 谁能告诉我为什么在尝试调用此函数时会出错? - Can anyone tell me why I get a error when i try to call this function? 我遇到两个错误,有人可以帮助我找到它们吗? - I am getting two errors, Can anyone help me find them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM