-
[MX04] Comparing values from array ... possible bug?
I'm trying to find the max value in an array and keep getting a value that isn't the max. Here's the function:
function maxValue(array) {
mxm = array[0];
for (i=0; i<array.length; i++) {
if (array[i]>mxm) {
mxm = array[i];
}
}
return mxm;
}
It's an array of about 15 values. The largest is 3984000, but the function returns 954000 as the max. In case it matters the array draws from an XML document. The only thing I can think of is that because 9>3 Flash sees the first integer and assumes that's the largest number, but it obviously isn't. Any ideas?
Thanks! Jared
-
i've seen this before when reading in values, they come in as text, not numbers. try 'forcing' it either when you read them in or when you compare them.
-
Looks to me like your values are getting compared as strings, rather than numbers. In a string comparison, 954000 is greater than 3984000 because the first character (9) is greater than the first character (3). Just like Zoo is greater than Apple. Instead, make sure your values are input as numbers, rather than strings.
Also, rather than...
Code:
if (array[i]>mxm) {
mxm = array[i];
}
...use...
Code:
mxm = Math.max(mxm, array[i]);
-
Bingo! Thanks y'all. Real quick: what's the best way to declare a variable type inside of an array?
I tried var myArray[i]:Number = 34342
... to no avail. Do i have to assign the number to a variable and then insert that variable in the array?
-
Flashmatics
you cant strict datatype array elements
-
would it be myArray[i] = Number(34342); i think what might be happening in your example is it makes the array var, typecasts it as Number then changes it to String (cause of a bug maybe, i had the same problems, once).
-
Code:
myArray[i] = Number(34342);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|