A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [MX04] Comparing values from array ... possible bug?

  1. #1
    Member
    Join Date
    Nov 2006
    Posts
    53

    [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

  2. #2
    Member
    Join Date
    May 2001
    Location
    concrete, WA
    Posts
    59
    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.

  3. #3
    The Ancient
    Join Date
    Mar 2000
    Location
    Michigan, USA
    Posts
    213
    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]);
    jahasloth

  4. #4
    Member
    Join Date
    Nov 2006
    Posts
    53
    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?

  5. #5
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    you cant strict datatype array elements
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  6. #6
    Member
    Join Date
    May 2001
    Location
    concrete, WA
    Posts
    59
    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).

  7. #7
    The Ancient
    Join Date
    Mar 2000
    Location
    Michigan, USA
    Posts
    213
    Code:
    myArray[i] = Number(34342);
    jahasloth

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center