So I want to do expression on variable instead of directly array element because I think
But when I attempt to swap 2 element in the array using variable, it don't work (see the code below).
I can't think of the keyword to this situation. Google results are all deep/shallow copy array.
Any explanation?
for (let i = 0, j = 1; i < numsClone.length && j < numsClone.length;) {
let numHasEvenIdx = numsClone[i];
let numHasOddIdx = numsClone[j];
if (numHasEvenIdx % 2 === 1 && numHasOddIdx % 2 === 0) {
[numsClone[i], numsClone[j]] = [numsClone[j], numsClone[i]];
// I want this to be [numHasEvenIdx, numHasOddIdx] = [numHasOddIdx, numHasEvenIdx]
i += 2;
j += 2;
}
else if (numHasEvenIdx % 2 === 0) i += 2;
else j += 2; //(numHasOddIdx % 2 === 1)
}