The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript [1,2,3,4,5,6,7,8,9].shuffle() Re: [1,2,3,4,5,6,7,8,9].shuffle()


It’s nice solution **shuffleArr.splice(pos, 0, elem);** I didn’t know how to make that elem go to different position, because Math.random() can repeat themselves, thats why there was this strange

if ( shuffleArr[pos] == undefined)

but, why you use:

pos = Math.floor( Math.random() * (shuffleArr.length + 1) )

this works too:

pos = Math.floor( Math.random() * len );

Thanks @CrocoDillon for your help