Open utterances-bot opened 3 years ago
const sum = (...numsList) => {
if(numsList.length == 0){
return 0;
}
let sum = 0;
for(let i =0 ;i <numsList.length;i++){
sum += numsList[i];
}
return sum;
}
console.log(sum(...[])); console.log(sum(...[1,1,1,1,1,1,9,6,5], 2,4,5,6,7,7, ...[]));
const sum = (...numsList) => { if(numsList.length == 0){ return 0; } let sum = 0; for(let i =0 ;i <numsList.length;i++){ sum += numsList[i]; } return sum; }
console.log(sum(...[])); console.log(sum(...[1,1,1,1,1,1,9,6,5], 2,4,5,6,7,7, ...[]));
Looks good. Thanks for sharing @Learn-Deepak!
Or with reduce:
const sum = (...numsList) => {
const sum = numsList.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0)
return sum;
}
Or with reduce:
const sum = (...numsList) => { const sum = numsList.reduce((accumulator, currentValue) => { return accumulator + currentValue; }, 0) return sum; }
Looks good @Calanthe! Thanks for sharing your solution.
Thank-you for this wonderful post. I learnt about Identity Element property & the reason why we get -Infinity in Math.max()
Why Math.max() Without Arguments Returns -Infinity
What is the reason that Math.max() utility function when being called without arguments returns -Infinity.
https://dmitripavlutin.com/javascript-math-max-infinity/