🐛 (shuffle) better shuffle array
This commit is contained in:
17
src/utils.ts
17
src/utils.ts
@@ -1,14 +1,17 @@
|
|||||||
export const shuffleArray = <T>(array: T[]) => {
|
export const shuffleArray = <T>(array: T[]) => {
|
||||||
const arrayCopy = [...array]
|
let currentIndex = array.length,
|
||||||
|
randomIndex
|
||||||
|
|
||||||
for (let i = array.length - 1; i > 0; i--) {
|
while (currentIndex !== 0) {
|
||||||
const j = Math.floor(Math.random() * (i + 1))
|
randomIndex = Math.floor(Math.random() * currentIndex)
|
||||||
const temp = array[i]
|
currentIndex--
|
||||||
array[i] = array[j]
|
;[array[currentIndex], array[randomIndex]] = [
|
||||||
array[j] = temp
|
array[randomIndex],
|
||||||
|
array[currentIndex]
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
return arrayCopy
|
return array
|
||||||
}
|
}
|
||||||
|
|
||||||
export const popNElement = <T>(array: T[], numberOfElements: number) => {
|
export const popNElement = <T>(array: T[], numberOfElements: number) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user