1
I have this matrix:
r = {
br : {x:2, y:0, z:4},
pr : {x:2, y:1, z:5},
ou : {x:1, y:1, z:6},
pl : {x:1, y:1, z:7},
di : {x:1, y:1, z:7}
}
w = 0.5
And that tie that makes a calculation:
for (var t = 0; t < r.length; t++) {
p = p + (r. *** .x * w) - (r. *** .y * (1-w));
console.log(p);
}
What’s the right shape on ***
? I don’t know how to reference the object correctly with the traveled index. I am asking this, because for example if I call the object r.br.x
it brings the result 2 (but I don’t know if there can that way either or the right way).
Do this with forEach
would be more appropriate and better? If possible also someone could show me an example how would be in this case?
Thank you very much! I wanted you to increment, is the logic of my code is wrong?
– Pedro
@Pedro The code increments the value of
p
, but if you do the table test will realize that when x and y are equal to 1, the value of(v.x * w) - (v.y * (1-w))
is equal to zero (the expression is(1 * 0.5) - (1 * (1 - 0.5))
, which is zero). Then for the keysou
,pl
anddi
(which have x and y equal to 1), the value added top
will be zero. If this is really the account you were supposed to do, then you’re right. Otherwise, just change the account, because the rest of the code (to go through all the values and add inp
) that’s right– hkotsubo
In fact, whenever x and y are equal, the value of
(v.x * w) - (v.y * (1-w))
will be zero - it happens becausew
is 0.5 then1 - w
is also 0.5, so when x and y are equal, the expression is(valor * 0.5) - (valor * 0.5)
, which will always be zero. I don’t know if this is your problem, but finally, the code provided goes through all the values of the object and adds inp
. Whether the accounts should go to zero or not is another story...– hkotsubo