mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-30 21:03:58 -05:00
12 lines
318 B
TypeScript
12 lines
318 B
TypeScript
/** Shuffle array using the Fisher–Yates shuffle algorithm */
|
||
export const shuffleArray = <T>(arrToShuffle: Array<T>) => {
|
||
const arr = [...arrToShuffle];
|
||
|
||
for (let i = arr.length - 1; i > 0; i--) {
|
||
const j = Math.floor(Math.random() * (i + 1));
|
||
[arr[i], arr[j]] = [arr[j], arr[i]];
|
||
}
|
||
|
||
return arr;
|
||
};
|