Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit db12012

Browse files
Igor IriantoKent C. Dodds
authored andcommitted
feat(sum): add sum to add all elements in array (#68)
* add sum to add all elements in array * feat(sum): Add a sum function
1 parent e66b6ce commit db12012

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import subtract from './subtract'
1818
import divide from './divide'
1919
import multiply from './multiply'
2020
import square from './square'
21+
import sum from './sum'
2122

2223
export {
2324
flatten,
@@ -40,4 +41,5 @@ export {
4041
divide,
4142
multiply,
4243
square,
44+
sum,
4345
}

src/pad-left.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ function padLeft(str, size, padWith) {
1818
return Array(size - str.length + 1).join(padWith || '0') + str
1919
}
2020
}
21-

src/sum.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default sum
2+
3+
/**
4+
* Original Source: http://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers
5+
*
6+
* This method will sum up all the array elements, assuming the elements are all numbers
7+
*
8+
* @param {Array} arr - the arr to be summed up
9+
*
10+
*/
11+
12+
function sum(arr) {
13+
return arr.reduce((a, b) => {
14+
return a + b
15+
}, 0)
16+
}

test/pad-left.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,3 @@ test('does not pad a string longer than the pad length', t => {
2525
const actual = padLeft(original, padLength)
2626
t.deepEqual(actual, expected)
2727
})
28-

test/sum.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import test from 'ava'
2+
import {sum} from '../src'
3+
4+
test('sums all array elements', t => {
5+
const array = [1,2,3,4,5]
6+
const expected = 1 + 2 + 3 + 4 + 5
7+
const actual = sum(array)
8+
t.deepEqual(actual, expected)
9+
})

0 commit comments

Comments
 (0)