I am attempting to populate array:
var array2 = ["a", "b", "c"];
With some random number/value, so that the value separates each original value.
However, using a for loop like this:
for (var am = 1; am < array2.length; am+2) {
array2.splice(am, 0, undefined);
}
Causes my browser to freeze, since on each splice the array2’s length mutates and causes an infinite loop so I attempted this method:
var l = array2.length;
for(var i = 1; i < l; i+2) {
array2.splice(i, 0, undefined);
}
Now I don’t know what’s going on. I assume l is mutating again.
I have to advance i for two because array2 after the first splice becomes:
["a", undefined, "b", "c"];
However, since i = 3 and I’ve set the expression i < l, the only explanation is that l is changing again.
Any explanation would be welcome. The end result of how I want the code to proceed is:
array2 = ["a", undefined, "b", undefined, "c"];
You’re not actually incrementing
amin that line. Change it to:Using
+=meansam = am + 2