could someone please explain, why the Code behaves this way? I wrote a test function to illustrate the issue I have on a greater scale in my code. I want "b" to keep the value [1, 2, 3] and then update "a" to [2, 2, 3] using the math.random function. But both get updated and I don't understand why. How can I save the value [1, 2, 3] in b without beingt updated?
let a = [1, 2, 3]
let b = []
function assignB() {
b = a
}
function reassignA() {
console.log(b) // [1, 2, 3]
let randomNumber = Math.floor(Math.random() * a.length)
if (a[randomNumber] == 1) {
a[randomNumber] = 2
console.log(b) // [2, 2, 3]
} else {
reassignA()
}
console.log(b) // [2, 2, 3]
}
function test() {
assignB()
reassignA()
}
test()