The k-means algorithm relies on two concepts in order to operate: average and distance. In order to tell you where the center of a cluster is, the algorithm will calculate an average value for these points. In this case, we will use the arithmetic mean, or the sum of values divided by the number of values, to represent our average. In ES5/classic JavaScript (I'm also being purposefully explicit in this example, for any readers who are not familiar with calculating the mean), we might write a function like this:
/**
* @param {Array.<number>} numbers
* @return {float}
*/
function mean(numbers) {
var sum = 0, length = numbers.length;
if (length === 0) {
/**
* Mathematically, the mean of an empty set is undefined,
* so we could return early here. We could also allow the function
* to attempt dividing 0/0, would would...