Javascript - Counting array elements by reduce method until specific value occurs doesn't give a correct output

后端 未结 4 1679
陌清茗
陌清茗 2021-01-29 01:34
const arr = [5,6,0,7,8];
const sum = (arr,num) => arr.reduce((total)=>(num==0 ? total : total+num), 0) 
console.log(sum(arr, 0))

Please check how

4条回答
  •  太阳男子
    2021-01-29 01:55

    You need parenthesis to execute the function ()

    sum(arr, 0)
    

    Without parenthesis you store a reference to the function in the variable

提交回复
热议问题