Darrow Hartman
Darrow Hartman

Reputation: 4383

Odd array formatting

I am participating in a CTF tournament and one of the problems has some js code with this line:

result[(j * LEN) + i] = bytes[(((j + shifter) * LEN) % bytes.length) + i]

Ignore all the variables. I am confused that an array results would have something equalling something in a value. In essence, I am confused about this:

Array[a = b]

Can someone explain why this is works?

Upvotes: 0

Views: 77

Answers (1)

Snow
Snow

Reputation: 4097

You have the nesting levels mixed up.

result[(j * LEN)   + i] = bytes[(((j + shifter) * LEN) % bytes.length) + i]
// original code above: it's equivalent to below:
result[(j * LEN)   + i] = rightHandSide
result[(jTimesLen) + i] = rightHandSide
result[jTimesLenPlusI ] = rightHandSide

It's just ordinary assignment to an index of the object or array.

Still, arr[a = b] would be legal too, just confusing; assignments resolve to expressions, so arr[a = b] assigns b to the (already existing) variable a, and then accesses the b index of arr (but doesn't do anything after accessing the index).

a = 3;
b = 5;
arr = [];

arr[a = b];

console.log(a);
console.log(arr);

Upvotes: 2

Related Questions