Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.
Closed
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
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -110,4 +111,5 @@ export {
median,
timeDifference,
isPrime,
swapElements,
}
20 changes: 20 additions & 0 deletions src/swapElements.js
Original file line number Diff line number Diff line change
@@ -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 {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) {
const b = list[y]
list[y] = list[x]
list[x] = b
}
11 changes: 11 additions & 0 deletions test/swapElements.test.js
Original file line number Diff line number Diff line change
@@ -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]);
});