簡體   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]));

您必須在sum方法中返回調用結果以reduce

此外,在reduce方法中,您將返回分配值的結果。 返回分配的結果可能會令人困惑,在這種情況下不需要這樣做。

考慮到所有這些,您的代碼應如下所示:

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM