-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSumArrays.js
50 lines (46 loc) · 1.06 KB
/
SumArrays.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Description:
*
* Write a function that takes an array of numbers
* and returns the sum of the numbers. The numbers can be
* negative or non-integer. If the array does not contain
* any numbers then you should return 0.
*
* Examples
* Input: [1, 5.2, 4, 0, -1]
* Output: 9.2
*
* Input: []
* Output: 0
*
* Input: [-2.398]
* Output: -2.398
*
* Assumptions
*
* You can assume that you are only given numbers.
* You cannot assume the size of the array.
* You can assume that you do get an array and if the array is empty, return 0.
*
* What We're Testing
*
* We're testing basic loops and math operations. This is
* for beginners who are just learning loops and math operations.
* Advanced users may find this extremely easy and can
* easily write this in one line.
*
* Kata URL: https://www.codewars.com/kata/53dc54212259ed3d4f00071c
*
* @param {*} numbers
* @returns
*/
function sum(numbers) {
let result = 0;
if (numbers.length === 0) {
return result;
}
numbers.forEach((element) => {
result += element;
});
return result;
}