Write a function `sum(...numbers)` that accepts any number of arguments and returns their sum.
Problem Statement
Write a function `sum(...numbers)` that accepts any number of arguments and returns their sum.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Rest parameters (...args) collect all arguments into an array.
š” Hint 2:
Use Array.prototype.reduce to sum all values.
š” Hint 3:
return numbers.reduce((acc, n) => acc + n, 0);
ā
Solution:
```javascript
function sum(...numbers) {
return numbers.reduce((acc, n) => acc + n, 0);
}
```