sum()
JavaScript
easy
15 mins
Design a function sum that can take any number of arguments and return their total. The function should work for both fixed and variable number of arguments using JavaScript features. Only numerical arguments will be provided.
Example Inputs & Outputs
sum(1, 2, 3) → 6 sum(10) → 10 sum() → 0 sum(5, -5, 10, 20) → 30 sum(100, 200, 300, 400) → 1000
Constraints & Edge Cases
- Inputs will always be numbers (integers or floats).
- No arguments → should return
0. - Function must handle a variable number of arguments.
- Negative numbers should be handled correctly.
- Must not use built-in
eval()orFunction()constructor.
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
