I was wondering why with some text put in console.log(): (example: console.log(" some " + arr) this function
works as designed and without it only works first time ??
FOR SIMPLICITY, shuffle change the order in a random way --> not exactly random but almost
I know that array is a reference type and I only give an address to calculate and I am aware that sort changes the original array.
WITH SOMETHING IN CONSOLE "text " + arrx
let arr = [1, 2, 3];
function shuffle(arrx){
arrx.sort(callback);
function callback(a,b){
let x = 0.49 - Math.random();
return x;
}
console.log("arx " + arrx);
}
shuffle(arr);
RESULT(example): arx 1,2,3
shuffle(arr);
RESULT(example): arx 1,2,3
shuffle(arr);
RESULT(example): arx 2,3,1
shuffle(arr);
RESULT(example): arx 2,3,1
shuffle(arr);
RESULT(example): arx 1,3,2
AND WITHOUT TEXT
let arr = [1, 2, 3];
function shuffle(arrx){
arrx.sort(callback);
function callback(a,b) {
let x = 0.49 - Math.random();
return x;
}
console.log(arrx);
}
shuffle(arr);
shuffle(arr);
shuffle(arr);
shuffle(arr);
shuffle(arr);
RESULT ALWAYS THE SAME example [1,2,3] or something else multiplied with every shuffle
Regards ;),