MCPcopy Create free account
hub / github.com/freeCodeCamp/freeCodeCamp / shuffleArray

Function shuffleArray

packages/shared/src/utils/shuffle-array.ts:2–14  ·  view source on GitHub ↗
(arrToShuffle: Array<T>)

Source from the content-addressed store, hash-verified

1/** Shuffle array using the Fisher–Yates shuffle algorithm */
2export const shuffleArray = <T>(arrToShuffle: Array<T>) => {
3 const arr = [...arrToShuffle];
4
5 for (let i = arr.length - 1; i > 0; i--) {
6 const j = Math.floor(Math.random() * (i + 1));
7
8 // We know that i and j are within the bounds of the array, TS doesn't
9 // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
10 [arr[i], arr[j]] = [arr[j]!, arr[i]!];
11 }
12
13 return arr;
14};

Callers 3

ShowQuizFunction · 0.90
generateRandomExamFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected