function randOrd() {
return (Math.round(Math.random()) - 0.5)
}
A = [0,1,2,3,4,5,6,7]
var B = A.sort(randOrd)
console.log('A=',A)
console.log('B=',B)
output:
a= [ 3, 4, 0, 1, 6, 2, 5, 7 ]
b= [ 3, 4, 0, 1, 6, 2, 5, 7 ]
I expected a
to be the original array and b
to be sorted. But they are both equal (sorted), why?
Because the Array.sort() method sorts in-place and then returns the array.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments