Easy
Sum All Numbers in an Array
Description
Your task is to write a function that takes an array of numbers as input and returns the sum of all the numbers in the array.
This problem is fundamental in programming and is commonly used in data processing tasks such as:
- Calculating totals (e.g., total revenue, total expenses).
- Computing averages by dividing the sum by the number of elements.
- Performing statistical operations like variance and standard deviation calculations.
Requirements
Input: An array containing integers or floating-point numbers.
Output: A single number representing the sum of all elements in the array.
Considerations
- The function should handle positive numbers, negative numbers, and zeros correctly.
- If the array is empty, return '0'.
- Edge cases like arrays with only zeros or negative numbers should be considered.
- The function should assume all elements are valid numbers (no need for type validation).
Example Usage
sumAllNumbers([1, 2, 3, 4, 5]);// Expected output: 15sumAllNumbers([-1, -2, -3, 4]);// Expected output: -2sumAllNumbers([0, 0, 0]);// Expected output: 0sumAllNumbers([]);// Expected output: 0
Did you know?
TypeScript is not a language, it is a superset of JavaScript.
Test Cases
Input:
[ 1, 2, 3, 4, 5 ]
Expected Output:
"15"