From 1b042897a662a431fff55a23d2f2ffb9050b973d Mon Sep 17 00:00:00 2001 From: turisap Date: Thu, 1 Mar 2018 16:39:33 +0700 Subject: [PATCH 1/2] feat(*): add swapElements function this is a completely new feature, swapElements swaps two elements at a given array at specified indexes * --- src/index.js | 2 ++ src/swapElements.js | 20 ++++++++++++++++++++ test/swapElements.test.js | 11 +++++++++++ 3 files changed, 33 insertions(+) create mode 100644 src/swapElements.js create mode 100644 test/swapElements.test.js diff --git a/src/index.js b/src/index.js index 2615a722..5d1b718f 100644 --- a/src/index.js +++ b/src/index.js @@ -53,6 +53,7 @@ import find from './find' import median from './array-median' import timeDifference from './timeDifference' import isPrime from './is-prime' +import swapElements from './swapElements' export { isOdd, @@ -110,4 +111,5 @@ export { median, timeDifference, isPrime, + swapElements } diff --git a/src/swapElements.js b/src/swapElements.js new file mode 100644 index 00000000..49c2e870 --- /dev/null +++ b/src/swapElements.js @@ -0,0 +1,20 @@ +/** + * Created by HP on 3/1/2018. + */ +export default swapElements; + + +/** + * Original source https://stackoverflow.com/questions/872310/javascript-swap-array-elements + * + * This function swaps elements at indexes 'a' and 'b' in array 'target' + * + * @param x {Number} - the first index in the array 'target' + * @param y {Number} - the second index in the array 'target' + * @param list {Array} - targeted array + */ +function swapElements(x,y, list){ + var b = list[y]; + list[y] = list[x]; + list[x] = b; +} \ No newline at end of file diff --git a/test/swapElements.test.js b/test/swapElements.test.js new file mode 100644 index 00000000..4ec9e0ad --- /dev/null +++ b/test/swapElements.test.js @@ -0,0 +1,11 @@ +/** + * Created by HP on 3/1/2018. + */ +import test from 'ava'; +import {swapElements} from '../src'; + +test('should swap two elements at given indexes in the targeted array', t => { + const target = [1,2,3]; + swapElements(1,2,target); + t.deepEqual(target, [1,3,2]); +}); \ No newline at end of file From 35622e3bdf9ee0e65fd25e1cd938c8d27cb5b27d Mon Sep 17 00:00:00 2001 From: turisap Date: Thu, 1 Mar 2018 16:48:52 +0700 Subject: [PATCH 2/2] style(*): ESlint corrections Corrections in code as removing semicolons, changing line breaks from CRLF to LF, adding whitespaces --- src/index.js | 2 +- src/swapElements.js | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 5d1b718f..eae1beb2 100644 --- a/src/index.js +++ b/src/index.js @@ -111,5 +111,5 @@ export { median, timeDifference, isPrime, - swapElements + swapElements, } diff --git a/src/swapElements.js b/src/swapElements.js index 49c2e870..1b7413fc 100644 --- a/src/swapElements.js +++ b/src/swapElements.js @@ -1,7 +1,7 @@ /** * Created by HP on 3/1/2018. */ -export default swapElements; +export default swapElements /** @@ -9,12 +9,12 @@ export default swapElements; * * This function swaps elements at indexes 'a' and 'b' in array 'target' * - * @param x {Number} - the first index in the array 'target' - * @param y {Number} - the second index in the array 'target' - * @param list {Array} - targeted array + * @param {Number} x - the first index in the array 'target' + * @param {Number} y - the second index in the array 'target' + * @param {Array} list - targeted array */ -function swapElements(x,y, list){ - var b = list[y]; - list[y] = list[x]; - list[x] = b; -} \ No newline at end of file +function swapElements(x, y, list) { + const b = list[y] + list[y] = list[x] + list[x] = b +}