Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:33:07+00:00 2026-06-01T09:33:07+00:00

I have a number array and I’d like to calculate the median. When the

  • 0

I have a number array and I’d like to calculate the median.
When the array is odd, the calculation is OK, when it’s even strange number comes up.

private var numbers:String = "2,5,3,4,6,1";
private var array:Array = numbers.split(",");

private function getMedian(array:Array):Number {
 var sortnums:Array = array.sort(Array.NUMERIC);
 var length:Number = sortnums.length;
 var mid1:Number; var mid2:Number; var median:Number;

 if(length % 2 == 0){
     mid1 = length / 2; trace("mid1: "+mid1);
 mid2= ((length - 1) / 2)-0.5; trace("mid2: "+mid2);
 trace ("mid1: "+sortnums[mid1]+", mid2: "+sortnums[mid2]);
 median = (sortnums[mid1] + sortnums[mid2]) / 2;
 }else{
 mid1 = (length / 2)-0.5
 median = sortnums[mid1]
 }
 trace (median);
 return median;
}

The result is 21.5, but should be 3.5

mid1 and mid2 are a position in the array.

Could somebody help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:33:09+00:00Added an answer on June 1, 2026 at 9:33 am

    Try this (for tidyness):

    function getMedian(plug:Array):Number
    {
        // Even length.
        if(plug.length % 2 == 0)
        {
            var a:Number = plug[int(plug.length / 2) - 1];
            var b:Number = plug[int(plug.length / 2)];
    
            return (a + b) / 2;
        }
    
        // Odd length.
        return plug[int(plug.length / 2)];
    }
    
    
    // Tests.
    trace(getMedian([2,5,3,4,6,1])); // 3.5
    trace(getMedian([2,5,3,4,6])); // 3
    

    By the way, I’m fairly certain your code is working fine, the problem is that you’re doing this as a string concatenation rather than a number sum here:

    median = (sortnums[mid1] + sortnums[mid2]) / 2;
    

    Meaning you’re literally adding the string 1.5 onto 2 and getting 21.5 rather than 3.5.

    Try amending your code so it reads like so:

    median = (Number(sortnums[mid1]) + Number(sortnums[mid2])) / 2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of numbers, each one of these number represents weight. the
I have array like below the page. I want to find array number which
Possible Duplicate: Non repeating random number array I have a String array with many
Say I have an array of number a <- c(1,2,3,6,7,8,9,10,20) if there a way
I have 3 arrays like this: 1st Array ( [0695] => Array ( [loan_number]
I have the following string fromat: session=11;reserID=1000001 How to get string array of number?
I have an array made of unlimited number of objects. These objects have the
I have a simple array with x number of items. I am displaying them
I have a 2 dimensional array forming a table: [color][number][shape ] ------------------------- [black][10 ][square
I have create an array containing person firstname,lastname and number which i populate in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.