How can i find median values from array in javascript
this is my array
var data = [
{ values: 4 },
{ values: 4 },
{ values: 4 },
{ values: 5 },
{ values: 2 },
{ values: 6 },
{ values: 6 },
{ values: 5 }
];
and i have tried this code
function findMedian(m) {
var middle = m.length - 1 / 2;
if (m.length - 1 % 2 == 1) {
return m[middle];
} else {
return (m[middle - 1] + m[middle]) / 2.0;
}
}
But it's return NaN
value
My calculation formula is
Find the median of the data. Place the number of data in ascending order. Then, mark the place whose value we take into account when calculating the median.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…