Having trouble understanding part of my code, The Fisher–Yates shuffle

Max

I'm learning about the "The Fisher Yates shuffle" and came across an issue. The part of my code that I've highlighted and commented "This Work", "This Doesn't Work" is to shuffle the array. From my understanding of Javascript, the part that doesn't work should work. Can you explain to me why it doesn't work?

const array = [..."12345678"]

let arrayShuffle = function(arr){


    for(let i = array.length - 1; i > 0; i--){
        let randomNum = Math.floor(Math.random() * (i + 1))
        let temp;

        let currentPos = arr[i] 
        let randomPos = arr[randomNum]

        /* This Work(it shuffled) \/ */
        temp = arr[i]
        arr[i] = arr[randomNum]
        arr[randomNum] = temp



        /* This Doesnt Work(Doesn't shuffle) \/ */
        // temp = currentPos
        // currentPos = randomPos
        // randomPos = temp
    }

    return arr
}

let res = arrayShuffle(array)
console.log(res)
Josh

This does not work, because of currentPos = arr[i] and randomPos = arr[randomNum]. Both of these are copying the values in arr, not the acctual pointer. Therefore, when you change currentPos or randomPos you are just changing a copy of whats actually in the array.

Take for example,

let x == 5
let y == x
y = y+1
console.log(x)
console.log(y) 

x = 5
y = 6

The same thing is going on here. when we let y = x, it is only copying the value in x, and it is not an actual reference back to x. Therefore when we change y, we are only changing y, leaving x unaffected.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I am having trouble understanding part of an ExceptionError code

Fisher–Yates Shuffle in Javascript

Having trouble understanding a portion of code

Having trouble understanding this assembly code

Shuffle an Array coding challenge. trouble understanding one part

Having trouble understanding .this keyword use in this code

Haskell - Having trouble understanding a small bit of code

Having trouble understanding a portion of code (bit operation)

I'm having trouble understanding why a single click in my code is selecting text

Understanding Fischer-Yates shuffle

Correctness of Fisher-Yates Shuffle Executed Backward

pure Knuth/Fisher-Yates shuffle in haskell

Bridging the gap between the Fisher Yates shuffle and mine

Having trouble deciphering my teachers pseudo code

Having trouble compiling my code in the remix IDE

Group by and having trouble understanding

Having trouble understanding currying

Having trouble understanding what this test case code is doing

Buffer to integer. Having trouble understanding this line of code

I'm having trouble understanding the syntax used in a piece of code

Having trouble understanding the upper bound and lower bound in the following R code

Having trouble processing a file from my documents in my code?

Having some trouble understanding middleware

Having trouble understanding lbu instruction

Having trouble understanding pointer operations

Having trouble understanding the map() method

Having trouble understanding call signatures

Having trouble understanding sequence diagrams

Having trouble understanding ajax calls