Files
freeCodeCamp/shared/utils/shuffle-array.ts
Huyen Nguyen 6f4488998a feat(client): quiz challenge with validation (#56163)
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2024-10-01 21:08:09 +02:00

12 lines
318 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** Shuffle array using the FisherYates 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;
};