You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are various ways to do it correctly, either Fisher–Yates or the "assign a random number to each element" method. The former is more efficient (and doesn't have a problem if the function returns the same value twice), but the latter is shorter to implement in javascript (and the number of input elements so low that it practically doesn't matter):
The function passed to sort is supposed to return the same value when given a specific pair of elements a and b as its two arguments (ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description)
prefix = prefix.split('').sort(function(){return 0.5-Math.random()}).join('')
does not meet this requirement. https://medium.com/@nitinpatel_20236/how-to-shuffle-correctly-shuffle-an-array-in-javascript-15ea3f84bfb has some commentary on why this is bad.
does not meet this requirement. https://medium.com/@nitinpatel_20236/how-to-shuffle-correctly-shuffle-an-array-in-javascript-15ea3f84bfb has some commentary on why this is bad.
There are various ways to do it correctly, either Fisher–Yates or the "assign a random number to each element" method. The former is more efficient (and doesn't have a problem if the function returns the same value twice), but the latter is shorter to implement in javascript (and the number of input elements so low that it practically doesn't matter):
prefix = prefix.split('').map(x=>[Math.random(),x]).sort().map(x=>x[1]).join('');
or non ES6 syntax:
prefix = prefix.split('').map(function(x){return[Math.random(),x]}).sort().map(function(x){return x[1]}).join('')
The text was updated successfully, but these errors were encountered: