• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Calculate Averages and Medians of Arrays

September 6, 2016 //  by Krishna Srinivasan//  Leave a Comment

Finding the average and the median of an array using JavaScript can be a fairly painless process. If you do it right, it should only take a few lines of code to calculate the correct averages and medians for any given array.

The average, which is the sum of all the values divided by the number of values, you’ll need to find the length of the array, sum up all of the values using the .reduce() method (the .reduce() method will reduce the values of an array to a single value by adding them all together), and divide the sum by the array’s length.

var values = [4, 9, 3, 17, 12] 
var sum = values.reduce((previous, current) => current += previous);
var avg = sum / values.length;
// avg = 9

To find the median or the value or values that lie in the middle of an array when the array is in numerical order, you need to .sort() the array to make sure that the values are properly arranged, then get the mean of the middle values, like this:

var values = [7, 6, 29, 3, 88, 11];
values.sort((a, b) => a - b);
var lowMiddle = Math.floor((values.length - 1) / 2);
var highMiddle = Math.ceil((values.length - 1) / 2);
var median = (values[lowMiddle] + values[highMiddle]) / 2;
// median = 20

Category: JavaScriptTag: Arrays, Average, Medians

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: «JavaScript Libraries to Keep Your Projects On-Trend JavaScript Libraries to Keep Your Projects On-Trend
Next Post: Being Oriented to the Sublime Text Plugins for JavaScript Developers Being Oriented to the Sublime Text Plugins for JavaScript Developers»

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact