Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
8
rated 0 times [  11] [ 3]  / answers: 1 / hits: 7694  / 4 Years ago, thu, march 12, 2020, 12:00:00


I want to shuffle an array in javascript...



I first wrote this function:



function shuffle(arr) {
for (i = 0; i < arr.length; i++) {
let temp0 = arr[Math.floor(Math.random() * arr.length)];
let temp1 = arr[Math.floor(Math.random() * arr.length)];
console.log(temp0); // select random item work correctly
console.log(temp1); // select random item work correctly
if (temp0 === temp1) { //for dont change arr[index] with save value!!!
continue;
}
temp2 = temp0;
temp0 = temp1;
temp1 = temp2;
console.log(temp0); //all temp0, temp1, temp2 are equal!!!
console.log(temp1); //all temp0, temp1, temp2 are equal!!!
console.log(temp2); //all temp0, temp1, temp2 are equal!!!
}
return arr
}


My algorithm is as follow:




  • select an item randomly

  • select another item randomly

  • switch the two items together



But I end up with temp0, temp1 and temp2 all equal!!!



Then I changed my code to this and it works perfectly



function shuffle1(arr) {
for (i = 0; i < arr.length; i++) {
x = Math.floor(Math.random() * arr.length);
y = Math.floor(Math.random() * arr.length);
if (x === y) { //for dont change arr[index] with self !!!
continue;
}
temp0 = arr[x];
arr[x] = arr[y];
arr[y] = temp0;
}
return arr
}


The only change that has happened is that: the randomly created number for index,is assigned to a variable and then this variable is used for selecting an item in array.



Can anyone help me understand why the first example does not work as expected in the second example?



Thanks in advance


More From » arrays

 Answers
1

The reason it does not update is because the numbers you are updating do not refer back to the elements in the array. Lets have a look in the node REPL.



We will create an array and try to update it both ways and see if the update was seen in the array.



> let a = [1,2,3]
undefined
> let f = a[0] // grab reference to 1st element
undefined
> f
1
> f = 2 // 1. try to update via our variable
2
> a[0]
1 // update has NOT worked
> a[0] = 2 // 2. try to update by array index
2
> a[0]
2 // update has worked


Once we change the first function to update via array indices both functions are the same.



When I update f I assign a new number object to it and break the link with a[0] which still refers to the old number. In js numbers are immutable so we cant update them even if we want to.



related:
Is number in JavaScript immutable?


[#4502] Monday, March 9, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
matteo

Total Points: 81
Total Questions: 100
Total Answers: 96

Location: Honduras
Member since Sat, Jul 24, 2021
3 Years ago
matteo questions
Tue, Mar 8, 22, 00:00, 2 Years ago
Sun, May 31, 20, 00:00, 4 Years ago
Tue, Jan 22, 19, 00:00, 5 Years ago
Wed, Sep 12, 18, 00:00, 6 Years ago
Sun, Jul 29, 18, 00:00, 6 Years ago
;