@oazmi/kitchensink - v0.9.13
    Preparing search index...

    Function shuffleArray

    • shuffle a 1D array via mutation. the ordering of elements will be randomized by the end.

      import { assertEquals, assertNotEquals } from "jsr:@std/assert"

      const
      range_100 = Array(100).fill(0).map((_, i) => (i)), // sequntially numbered array
      my_arr = range_100.slice()
      shuffleArray(my_arr) // shuffling our array via mutation

      // the shuffled array is very unlikely to equal to the original unshuffled form
      assertNotEquals(my_arr, range_100)
      // sort the shuffled array to assert the preservation of the contained items
      assertEquals(my_arr.toSorted((a, b) => (a - b)), range_100)

      Type Parameters

      • T

      Parameters

      • arr: T[]

      Returns T[]