Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/array-multiplier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default arrMultiply


/**
* Original source: https://stackoverflow.com/questions/8454977/
* how-do-i-multiply-each-member-of-an-array-by-a-scalar-in-javascript
* This method will perform Array multiplication with a number.
*
* @param {Array} array - Array to be multipled with a number
* @param {Number} multiplier - number to be multiply to each element of aray
* @return {Array} - Result of multiplication
*/
function arrMultiply(array, multiplier) {
return array.map(x => x * multiplier)
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import truncate from './truncate'
import validateEmail from './validateEmail'
import removeElementByIndex from './removeElementByIndex'
import clone from './clone'
import arrMultiply from './array-multiplier'

export {
reverseArrayInPlace,
Expand Down Expand Up @@ -184,4 +185,5 @@ export {
hex2hsl,
removeElementByIndex,
clone,
arrMultiply,
}
12 changes: 12 additions & 0 deletions test/array-multiplier.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import test from 'ava'
import {
arrMultiply,
} from '../src'

test('Multiply an array with a scaler number', t => {
const array = [1, 2, 3, 4]
const multiplier = 2
const expected = [2, 4, 6, 8]
const actual = arrMultiply(array, multiplier)
t.deepEqual(actual, expected)
})