# 10 examples of 'javascript math average' in JavaScript

Every line of 'javascript math average' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

## All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
``135function average(values) {136  let total = 0.0;137  let count = 0;138139  values.forEach(v => {140    if (missing(v)) {141      return;142    }143144    total += v;145    count += 1;146  });147148  if (count === 0) {149    return null;150  }151152  return total / count;153}``
``772function average(array) {773  var sum = 0;774  for (var i = 0; i < array.length; i++) {775    sum += Math.round(array[i]);776  }777  return (sum / array.length);778}``
``73function average(array) {74  function plus(a, b) {75    return a + b76  }7778  return array.reduce(plus) / array.length79}``
``28get average() {29    if (this.v.length < this.minsize) return -1;30    else return this.sum/this.v.length;31}``
``112function average(array) {113    if (array.length === 0) {114        return 0;115    }116    return (117        array.reduce(function(s, v) {118            return v + s;119        }, 0) / array.length120    );121}``
``316function average(data) {317    var sum = data.reduce(function(sum, value) {318        return sum + value;319    }, 0);320321    var avg = sum / data.length;322    return avg;323}``
``1064function Average(obj) {1065	var point = null;1066	if (typeof obj == "function") {1067		point = new AverageFuncPoint(obj);1068	} else if (typeof obj == "string") {1069		point = new AveragePropPoint(obj);1070	} else {1071		point = new AveragePoint();1072	}1073	this.run(point);1074	if (point.items == 0) return 0;1075	return point.total / point.items;1076}``
``60function average (arr) {61  var sum = 0, j = 0;62  for (var i = 0; i < arr.length; i++) {63    sum += arr[i]; ++j;64  }65  return j ? sum / j : 0;66}``
``3function average(list) {4  if (!list.length)5    return 0;67  var sum = list.reduce(function(previous, current) {8    return previous + current;9  });10  return (sum / list.length).toFixed(0);11}``
``61this.average = function average( column_name, conditions, callback ){62	var options = {}63		, column_name = column_name || 'id'64		, conditions = conditions || {};65	/* Reload the cache */66	if (conditions.force)	force = ActiveSupport.clone(conditions.force); delete conditions.force;67	if(typeof(column_name)=="function"){68		callback = column_name;69		column_name = 'id';70	}71	if(typeof(conditions)=="function"){72		callback = conditions;73		conditions = {};74	}75	options = {76		from: this.to_s(),77		select: "AVG("+ column_name +")",78		conditions: conditions79	}80	if (this._extra_parameters) options.extras = this._extra_parameters;81	var query = new Query().make_select(options);82	this.exec(query.sql, query.values, function (error, data){83		return callback ? callback(error, data[0][options.select]) : data;84	} );85	return this;86}``