Skip to content

Commit

Permalink
add: arrayRandom function
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Jul 14, 2022
1 parent cedd102 commit cbdb864
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pages:
- npm run jsDoc
- rm -rf public
- cp -r ./docs public
- cp -r ./examples public/examples

artifacts:
paths:
Expand Down
13 changes: 13 additions & 0 deletions src/arrayRandom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* This function will randomize the position of the array items. (change the original array!)
*
* @example arrayRandom(["A", "B", "C"]); // ["B","A","C"]
* @example arrayRandom([1,2,3,4,5,6,7,8,9]); // [8,5,1,4,3,6,9,2,7]
* @example arrayRandom([{a:1},{b:2},{c:3}]); // [{a:1},{c:3},{b:2}]
*
* @param {any[]} array - the array with the items to randomize
* @returns {any[]} the array changed
*/
export function arrayRandom(array) {
return array.sort(() => Math.random() - 0.5);;
}

0 comments on commit cbdb864

Please sign in to comment.