I'm writing in JavaScript and detected this strange behavior that I can't explain.
for (i in bubbles){
bubbles[i].string = "some stuff!" // <- no errors here
results[0] = i - 1
results[1] = i + 1
results[2] = parseInt(i) + 1
}
when i = 1 this happens
results[0] -> 0
results[1] -> 11
results[2] -> 2
is this even possible?! Maybe it's due to other errors in the code. I tried to isolate the case above but, if you need it, here's the whole code
for (i in bubbles){
if (bubbles[i].check()){
// define which boubble has been clicked and start dragging
bubbleDrag[2] = bubbles[i].check();
bubbleDrag[1] = i;
bubbleDrag[0] = true;
// define where to check to avoid overlapping dates
if (i != 0 && i < bubbles.length - 1){
bubbleDrag[3] = i - 1;
bubbleDrag[4] = i + 1;
} else if (i == 0 && bubbles.lenght > 1){
bubbleDrag[3] = i + 1;
} else if (i == bubbles.lenght - 1){
bubbleDrag[3] = i - 1;
}
}
}
bubbles? Afor -inloop in JavaScript is intended to iterate over objects{}, not over arrays[]. The iterator variable will always be a string.for...inloop, the loop variable is always a string, because you are iterating over object properties. If you are iterating over an array, use a normalforloop. See developer.mozilla.org/en-US/docs/JavaScript/Reference/… and stackoverflow.com/q/2265167/218196.for (var i=0; i<bubbles.length; i++)