我有问题添加一个数组的所有元素以及平均它们。我将如何做到这一点,并实现它与我目前的代码?元素的定义如下所示。

<script type="text/javascript">
//<![CDATA[

var i;
var elmt = new Array();

elmt[0] = "0";
elmt[1] = "1";
elmt[2] = "2";
elmt[3] = "3";
elmt[4] = "4";
elmt[5] = "7";
elmt[6] = "8";
elmt[7] = "9";
elmt[8] = "10";
elmt[9] = "11";

// Problem here
for (i = 9; i < 10; i++){
  document.write("The sum of all the elements is: " + /* Problem here */ + " The average of all the elements is: " + /* Problem here */ + "<br/>");
}   

//]]>
</script>

当前回答

使用reduce和ES6计算平均值(mean):

const average = list => list.reduce((prev, curr) => prev + curr) / list.length;

const list = [0, 10, 20, 30]
average(list) // 15

其他回答

首先定义我们计划使用的所有变量。您将注意到,对于数字数组,我使用了[]的文字符号,而不是构造函数方法array()。此外,我使用了一个更短的方法来将多个变量设置为0。

var numbers = [], count = sum = avg = 0;

接下来,我用0到11的值填充空数字数组。这是为了回到最初的起点。注意我是如何推入数组count++的。这将推动count的当前值,然后在下一次增加该值。

while ( count < 12 )
    numbers.push( count++ );

最后,我为数字数组中的每个数字执行一个函数。这个函数一次处理一个数字,我在函数体中将其标识为“n”。

numbers.forEach(function(n){
  sum += n; 
  avg = sum / numbers.length;
});

最后,我们可以将sum值和avg值输出到控制台,以便查看结果:

// Sum: 66, Avg: 5.5
console.log( 'Sum: ' + sum + ', Avg: ' + avg );

在http://jsbin.com/unukoj/3/edit上看到它的行动

这是我的一个简单的求平均值的新手方法。希望这能帮助到一些人。

function numAvg(num){
    var total = 0;
    for(var i = 0;i < num.length; i++) { 
        total+=num[i];
    }
    return total/num.length;
}

让我们假设我们有一个这样的整数数组:

var values = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

用下面的公式求平均值

A = (1 / n)Σ喜(i = 1到n) ...同在所以:x1/n + x2/n + 0 ..+ n / n

我们将当前值除以值的数量,并将之前的结果添加到返回值中。

reduce方法签名为

reduce(callback[,default_previous_value])

reduce回调函数接受以下参数:

p:结果 之前计算的 c:当前值(来自当前索引) i:当前数组元素的索引值 a:当前缩减Array

第二个reduce的参数是默认值…(用于数组为空的情况)。

所以平均减法是:

var avg = values.reduce(function(p,c,i,a){return p + (c/a.length)},0);

如果你喜欢,你可以创建一个单独的函数

function average(p,c,i,a){return p + (c/a.length)};
function sum(p,c){return p + c)};

然后简单地引用回调方法签名

var avg = values.reduce(average,0);
var sum= values.reduce(sum,0);

或增强阵列原型直接..

Array.prototype.sum = Array.prototype.sum || function (){
  return this.reduce(function(p,c){return p+c},0);
};

可以在每次调用reduce方法时对该值进行除法。

Array.prototype.avg = Array.prototype.avg || function () {
  return this.reduce(function(p,c,i,a){return p+(c/a.length)},0);
};

或者更好,使用之前定义的array . prototype .sum()

方法,优化我只调用一次事业部的过程:)

Array.prototype.avg = Array.prototype.avg || function () {
  return this.sum()/this.length; 
};

然后在作用域的任意Array对象上:

[2, 6].avg();// -> 4
[2, 6].sum();// -> 8

注意:在我看来,一个返回NaN愿望的空数组比0更正确,在特定的用例中可能有用。

一种狡猾的方法可以做到这一点,尽管它确实需要使用(非常讨厌的)eval()。

var sum = eval(elmt.join('+')), avg = sum / elmt.length;
document.write("The sum of all the elements is: " + sum + " The average of all the elements is: " + avg + "<br/>");

我只是想把这个作为“盒子外”的选项之一。你永远不知道,狡猾可能会给你加分(或扣分)。

这里是一个快速添加到“数学”对象在javascript中添加一个“平均”命令!!

Math.average = function(input) {
  this.output = 0;
  for (this.i = 0; this.i < input.length; this.i++) {
    this.output+=Number(input[this.i]);
  }
  return this.output/input.length;
}

然后我有这个加法“数学”对象得到和!

Math.sum = function(input) {
  this.output = 0;
  for (this.i = 0; this.i < input.length; this.i++) {
    this.output+=Number(input[this.i]);
  }
  return this.output;
}

那么你要做的就是

alert(Math.sum([5,5,5])); //alerts “15”
alert(Math.average([10,0,5])); //alerts “5”

我把占位符数组只是传递在你的变量(输入如果他们是数字可以是一个字符串,因为它解析到一个数字!)