In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "JavaScript how to find the median of an array". The explanation in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "JavaScript how to find the median of an array" together.
Methods: 1. Sort the array and find the array length;2. Determine whether the array length is even or odd. If it is even, the median is "(array name [(array length)/2]+ array name [(array length)/2)+1])/2". If it is odd, the median is "array name [(array length/2)-0.5]".
Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.
A median is the number in the middle of an ordered set of data, representing a value in a sample, population, or probability distribution that divides the set of values into equal upper and lower parts. For a finite number set, you can find the median by ranking all observations high and low. If there are an even number of observations, the median is usually taken as the average of the two values in the middle.
JavaScript How to find the median of an array
Realization of ideas:
Sort the array and find the array length
If the array length is even, then the median will be arr[(arr.length)/2] +arr[(arr.length)/2)+1]/ 2.
If the array length is odd, the median will be the middle element.
Implementation code:
function medianof2Arr(arr1) { var concat = arr1; concat = concat.sort( function(a, b) { return a - b }); console.log(concat); var length = concat.length; if (length % 2 == 1) { //if the length is odd console.log("Median is: "+(concat[(length / 2)-0.5])) } else { //if the length is even console.log("Median is: "+(concat[length / 2]+concat[(length / 2) - 1]) / 2); }}arr1 = [1, 4, 7, 9,2]medianof2Arr(arr1);
arr1 = [1, 4, 7, 9]medianof2Arr(arr1);
Thank you for reading, the above is the content of "JavaScript how to find the array median", after the study of this article, I believe that everyone has a deeper understanding of JavaScript how to find the array median, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 220
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.