Node.js variables being changed without any operations performed on them

I am using a node.js server for a multiplayer synchronized dice, but i am having some strange problems with variables changing that are not referenced or used...

    var origonalRolls = rolls;

    //identify epic fails
    var epicFails = [];
    for(var r = 0; r < rolls.length; r++)
        if(rolls[r] === 1)
            epicFails.push(r);

    console.log("TEST 1 :: " + JSON.stringify(rolls));
    console.log("TEST 2 :: " + JSON.stringify(origonalRolls));
    //remove epic fails and the corresponding heighest
    if(epicFails.length > 0){
        for(var i = epicFails.length-1; i >= 0; i--){
            rolls.splice(epicFails[i], 1);
            if(rolls[rolls.length-1] >= success)
                rolls.splice(rolls.length-1, 1);
        }
    }
    console.log("TEST 3 :: " + JSON.stringify(rolls));
    console.log("TEST 4 :: " + JSON.stringify(origonalRolls));

the above should find any element in the rolls array which is 1 and then add it to epicFails. it should then remove it from rolls as well as the heighest remaining roll. (note, rolls is sorted numerically)

for some reason the output of this segment of code is as follows:

TEST 1 :: [1,1,2,3,3,6,7,7,9,9]
TEST 2 :: [1,1,2,3,3,6,7,7,9,9]
TEST 3 :: [2,3,3,6,7,7]
TEST 4 :: [2,3,3,6,7,7]

I am unsure why rolls and origonalRolls start the same and end the same. I am only using rolls.

Any help and/or explanation to this problem is welcome, it's been troubling me for a long time now...

In Javascript Arrays and Objects are only shallow copied - which means that an array (rolls) copied from another array (originalRolls) is only a reference to originalRolls - it is not an entirely new array, and modifying values in one will affect values in the other.

You will need to implement a deep copy function to create an entirely new array based off another. There are numerous imlementations of deep copying arrays/objects both here and elsewhere on the net - here is one of them from a quick Google.

Replace var origonalRolls = rolls; with:

var origonalRolls = [];
for (var i = 0, len = rolls.length; i < len; i++) {
    origonalRolls[i] = rolls[i];
}