1

I'm new to programming and javascript and am having trouble changing a value in a one of my arrays throught its index. The rest of the code works fine but I can't seem to access my faces[] through its index, while in another array it works fine.

f1 = 0;
f2 = 0;
f3 = 0;
f4 = 0;
f5 = 0;
f6 = 0;
faces = [f1, f2, f3, f4, f5, f6];

//loop through a single throw with 5 dies
for(var i = 0; i < dice.length; i++){
    var die = Math.floor(Math.random() * 6) + 1;

    //if hold is true skip the corresponding iteration
    if(hold1 == true && i===0){
        alert("holding: " + held[0]);//shows the value of the die that's being held
        for(var i = 0; i < 6; i++){
            faces[i] = 5;//doesn't add anything
            faces[2] = 5; //nor does this
            faces[1]++; //or this
            f1 = 3; //this works however
        }
        alert("faces are: " + f1 + ", " + f2 + ", " + f3 + ", " + f4 + ", " + f5 + ", " + f6);
        continue; 
    }
//rest of code

The alert is to check if the values change and it returns 3, 0, 0, 0, 0, 0 since only assigning it to the variables in the index directly seems to work.

GomuGomu
  • 13
  • 2
  • 5
  • why not use the array directly? – Nina Scholz Jan 11 '17 at 20:36
  • 2
    Your inner loop shouldn't use the same "i" variable as your outer loop. Change the variable name. Also, the inner condition, are you sure it is correct? `hold1 == true && i == 0` that looks suspiciously wrong. – Stephen Quan Jan 11 '17 at 20:37

4 Answers4

2

It looks like your code is expecting the variables to be updated when you update the faces at an array index, but faces[1] = 5 assigns the value 5 to faces[1], replacing the value (0) initially copied from f1. Try again, using the indexes only, but this time change your alert to:

alert("faces are: " + faces.join(','));

Also, as @StephenQuan points out, you need to use a different index variable for the inner loop

Rob M.
  • 35,491
  • 6
  • 51
  • 50
2

You do not change the value of fx as they are simple datatypes (int), they are called by value. Simple types are not objects, which can be called by reference. See this question: Is JavaScript a pass-by-reference or pass-by-value language?

f1 = 0;
f2 = 0;
f3 = 0;
f4 = 0;
f5 = 0;
f6 = 0;
faces = [f1, f2, f3, f4, f5, f6];

for(var i = 0; i < 6; i++){
  faces[i] = faces[i] + 5;//doesn't add anything
  faces[2] = faces[2] + 5; //nor does this
  faces[1] = faces[1]++; //or this
  f1 = 3; //this works however
}
console.log("faces are: " + f1 + ", " + f2 + ", " + f3 + ", " + f4 + ", " + f5 + ", " + f6);
console.log("faces are:" , faces);
Community
  • 1
  • 1
ppasler
  • 3,579
  • 5
  • 31
  • 51
  • @GomuGomu you are losing reference to your f-named variables when you set them via `faces[1] = `. This now means that indexed item no longer points to `f1`. – jusopi Jan 11 '17 at 20:43
0

See the section on var hoisting. You are using var i in the inner loop as well as the outer loop. Change that and see if it helps the flow. Also I would suggest if possible using let instead of var if you are using ECMA 6.

Yathi
  • 1,011
  • 1
  • 12
  • 21
0

This is because you are passing a reference to an object into the array. when you are first initializing the array.

Inside the loop you are executing

faces[i] = 5

you are replacing the reference to object f1 to value 5.

Please try the following.

faces = [{value: 0}, {value: 0}, {value: 0}, {value: 0}, {value: 0}, {value: 0}];

faces[i].value = 5;

alert("faces are: " + face[0].value+ ", " + face[1].value+ ", " + face[2].value+ ", " + face[3].value+ ", " + face[4].value+ ", " + face[5].value);
Adrian Bratu
  • 498
  • 2
  • 9
  • The array is initialized with values of type "number" copied from variables `f1` through `f6` which are all zeroes. – traktor Jan 11 '17 at 21:56