Solved

median of 2 numbers. how to?

Posted on 2013-05-16
6
332 Views
Last Modified: 2013-05-31
I have 2 numbers and trying to find median of those two eithout taking average manually but with built in functions. Any functions in javascript or prolog?

REGARDS.
0
Comment
Question by:jazzIIIlove
6 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 375 total points
ID: 39172056
I don't think there is a built in median function in JavaScript. If you just want the median of two numbers, then it is the same as the mean (average), just (num1+num2)/2

Here is a function for median in JavaScript
http://rosettacode.org/wiki/Averages/Median#JavaScript
And one for Prolog on the same page
http://rosettacode.org/wiki/Averages/Median#Prolog
0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 375 total points
ID: 39172066
The reason that it isn't usually a built in function is that it requires sorting the data to find the median. The most efficient sorts are "in-place" (i.e. they move the data around in the original array) so you would need to copy the array if you wanted to find the median without rearranging everything.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 39172370
Please assume they are sorted and only 2 numbers.


Regards.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 375 total points
ID: 39172593
function median(a, b) {
    return (a+b)/2;
}

Open in new window


Or if they are in an array
function median_array_of_2(ary) {
    return (ary[0]+ary[1])/2;
}

Open in new window

0
 
LVL 32

Expert Comment

by:sarabande
ID: 39174101
the definition of median function for an even number of values may be arbitrary. sometimes, it is required that the median value matches at least one of the values of the set. if so, for an even count you may have two medians (low and high) which are the two middle values of the sorted set.

Sara
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 125 total points
ID: 39203738
A Median is the mid-point of a "sample" but when the "sample" is an even number no true mid-point exists. The most typical way to treat this is to then take the mean (average) of the 2 points being before and after the mid-point. Some approaches differ and will choose either the 'before' value or the 'after' value instead of using the mean of those 2 points.

The statistical significance of a sample that is only of 2 values is so low that you really have not much choice except to use the average as any other technique applied would just produce an absurd bias.

http://en.wikipedia.org/wiki/Median
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now