alert("0123456789".split("").sort(function(){return .5-Math.random()}).join(""));
If you're looking for a quick way to generate a random 10-digit number in JavaScript with the digits from 0 to 9 in a mixed-up order, this method is one of the shortest options available. For example, it could produce a number like 7205169483.
split("")//This function splits the string into individual characters
.sort(function(){return .5-Math.random()})
.join("")//Joins the characters back together with an empty separator
Have you ever wondered why {return Math.random()}
wouldn't work in this context?
Do you know how many times the .sort()
function is executed - just once or ten times sequentially?