Fixing an array index's out of range error
Copy link to clipboard
Copied
Hi,
Am stuck in an eLearning project that I am working on and need some help fixing a 2007 error that seems to point to an index for an array being out of range.
Here is the previous posting where there are more details: http://forums.adobe.com/message/4685789#4685789
With much help (thank you once again kglad), the script looks like:
var index:int = 0;var numToDisplay:int = 10;var groupA:Array = ["1","2","3"]var groupB:Array = ["4","5","6"]var groupC:Array = ["7","8","9"]var word_array:Array = [];var i:int;for(i=0;i<3;i++){word_array.push(groupA[groupA.length*Math.random()]);}for(i=0;i<3;i++){word_array.push(groupB[groupB.length*Math.random()]);}for(i=0;i<3;i++){word_array.push(groupC[groupC.length*Math.random()]);}
This is used in another part of the script:
function randomWord() { if(index<numToDisplay){ wordT.text=word_array[index++]; t.reset(); t.start(); } else { wordT.text= ""; // display score } }
The error message (2007) points to:
wordT.text=word_array[index++];
When I trace (word_array) I get
,,,,,,
,,,,,,,
,,,,,,,,
Out of range index for this array?
How can I fix this out of range index?
Any help appreciated.
Copy link to clipboard
Copied
Since there appear to be only 9 elements getting added to the array, and your numDisplay is set to be 10, your condition ( if(index<numToDisplay){ ) allows index++ to get to 10, which will be out of range for your array. See if changing the value of numDisplay to 9 solves the problem.
Copy link to clipboard
Copied
Thanks, Ned for this suggestion.
Changing to 9 does not prevent the error.
Copy link to clipboard
Copied
(that's my fault. i forgot to include Math.floor to adjust those random numbers to ints.)

