Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move array functions to a folder #89
- Loading branch information
1 parent
1974e18
commit 237cbf4
Showing
21 changed files
with
123 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Check if all Elements of the array are equal | ||
* | ||
* @example | ||
* allEqual([0,0,0,0]) // true | ||
* allEqual([0,0,0,1]) // false | ||
* allEqual([0,"a","a"]) // false | ||
* allEqual([[1,1],[1,1]]) // true | ||
* allEqual([[1,1],[1,0]]) // false | ||
* allEqual([{a:"b",c:1},{a:"b"}]) // false | ||
* allEqual([{a:"b",c:1},{a:"b",c:1}]) // true | ||
* allEqual([{a:"b",c:1},{a:"b",c:1},{a:"c",c:1}]) // false | ||
* allEqual([{a:"b",c:1},{a:"b",c:1},{a:"b",c:1}]) // true | ||
* | ||
* @param {any} arr - the array to check all elements | ||
* @returns {boolean} true if all elements of the array ara equal | ||
*/ | ||
export function allEqual(arr) { return arr.every(v => JSON.stringify(v) === JSON.stringify(arr[0])); }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { randomInt } from "./randomNumber"; | ||
import { randomInt } from "../randomNumber"; | ||
|
||
/** | ||
* Returns a random item from the array | ||
* | ||
* @see {@link https://www.w3schools.com/python/ref_random_choice.asp} | ||
* | ||
* @example arrayChoice(["A", "B", "C"]); // "B" | ||
* @example arrayChoice([10, 5, 123]); // 10 | ||
* @example choice(["A", "B", "C"]); // "B" | ||
* @example choice([10, 5, 123]); // 10 | ||
* | ||
* @param {any[]} array - the array to select a random item | ||
* @returns {any} | ||
*/ | ||
export function arrayChoice(array) { | ||
export function choice(array) { | ||
return array[randomInt(array.length)]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { allEqual } from "./allEqual.js"; | ||
import { choice } from "./choice.js"; | ||
import { findBigObject } from "./findBigObject.js"; | ||
import { findLowObject } from "./findLowObject.js"; | ||
import { isSorted } from "./isSorted.js"; | ||
import { moveLeft, moveRight } from "./Move.js"; | ||
import { random } from "./random.js"; | ||
import { shuffle } from "./shuffle.js"; | ||
import { sortAscending } from "./sortAscending.js"; | ||
import { sortAscendingObject } from "./sortAscendingObject.js"; | ||
import { sortDescending } from "./sortDescending.js"; | ||
import { sortDescendingObject } from "./sortDescendingObject.js"; | ||
|
||
export { | ||
allEqual, | ||
choice, | ||
findBigObject, | ||
findLowObject, | ||
isSorted, | ||
moveLeft, moveRight, | ||
random, | ||
shuffle, | ||
sortAscending, sortAscendingObject, | ||
sortDescending, sortDescendingObject, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Check if the given array is sorted fom lowest to highest | ||
* | ||
* @example | ||
* isSorted([]) // true | ||
* isSorted([0,0,0,0]) // true | ||
* isSorted([2,1,4]) // false | ||
* isSorted([3,2,1]) // false | ||
* isSorted([1,2,3]) // true | ||
* isSorted(["B","A","D"]) // false | ||
* isSorted(["A","B","C"]) // true | ||
* | ||
* @param {any} arr - the array to check | ||
* @returns {boolean} true if the array is sorted | ||
*/ | ||
export function isSorted(arr = []) { | ||
for (let i = 0; i < arr.length - 1; i++) { | ||
if (arr[i] > arr[i + 1]) return false; | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 random(["A", "B", "C"]); // ["B","A","C"] | ||
* @example random([1,2,3,4,5,6,7,8,9]); // [8,5,1,4,3,6,9,2,7] | ||
* @example random([{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 random(array) { | ||
return array.sort(() => Math.random() - 0.5); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.