I was under the impression that I knew how to declare JavaScript arrays, but in this particular script, I seem to be stuck in an infinite loop of undefined
elements within the array.
In my code, I define three arrays containing numbers—two with multiple values and one with a single value.
There's a switch statement that assigns one of these arrays to a new variable called cluster_array
.
However, when I try to iterate through cluster_array
using a for
loop, I encounter an endless loop where all elements are undefined
. What could I possibly be overlooking?
<script type="text/javascript">
var ga_west_cluster = new Array(10,11,12,14,74,75,76,77,78,79,80,81,82,83,85,86,87,88,89,90,91,92,295,296);
// original problematic array
var ga_east_cluster = new Array(84);
// solved by adding an extra (dummy) value
var ga_east_cluster = new Array(1,84);
var sc_cluster = new Array(93,94,95,96,97,98,99,100,101,102,103);
</script>
The following is the content of the alert
:
var test_message = "cluster data\n";
for(var k=0;k<cluster_array.length;k++)
test_message += "value: "+cluster_array[k]+"\n";