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

Commit 38b6e73

Browse files
vinodkhandelwalKent C. Dodds
authored and
Kent C. Dodds
committed
feat(arrMultiplier): add multiplier to Array (#241)
1 parent 38869bd commit 38b6e73

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Diff for: src/array-multiplier.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default arrMultiply
2+
3+
4+
/**
5+
* Original source: https://stackoverflow.com/questions/8454977/
6+
* how-do-i-multiply-each-member-of-an-array-by-a-scalar-in-javascript
7+
* This method will perform Array multiplication with a number.
8+
*
9+
* @param {Array} array - Array to be multipled with a number
10+
* @param {Number} multiplier - number to be multiply to each element of aray
11+
* @return {Array} - Result of multiplication
12+
*/
13+
function arrMultiply(array, multiplier) {
14+
return array.map(x => x * multiplier)
15+
}

Diff for: src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import truncate from './truncate'
9090
import validateEmail from './validateEmail'
9191
import removeElementByIndex from './removeElementByIndex'
9292
import clone from './clone'
93+
import arrMultiply from './array-multiplier'
9394

9495
export {
9596
reverseArrayInPlace,
@@ -184,4 +185,5 @@ export {
184185
hex2hsl,
185186
removeElementByIndex,
186187
clone,
188+
arrMultiply,
187189
}

Diff for: test/array-multiplier.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import test from 'ava'
2+
import {
3+
arrMultiply,
4+
} from '../src'
5+
6+
test('Multiply an array with a scaler number', t => {
7+
const array = [1, 2, 3, 4]
8+
const multiplier = 2
9+
const expected = [2, 4, 6, 8]
10+
const actual = arrMultiply(array, multiplier)
11+
t.deepEqual(actual, expected)
12+
})

0 commit comments

Comments
 (0)