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

<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>

当前回答

平均最短的一行

const avg = arr => arr.reduce((acc,v,i,a)=>(acc+v/a.length),0);

Sum的最短一行

const sum = arr => arr.reduce((a,b)=>a+b);

其他回答

在常绿浏览器上,你可以使用箭头函数 Avg = [1,2,3].reduce((a,b) => (a+b);

运行10万次,for循环方法和reduce方法之间的时间差可以忽略不计。

s=Date.now();for(i=0;i<100000;i++){ n=[1,2,3]; a=n.reduce((a,b) => (a+b)) / n.length }; console.log("100k reduce took " + (Date.now()-s) + "ms."); s=Date.now();for(i=0;i<100000;i++){n=[1,2,3]; nl=n.length; a=0; for(j=nl-1;j>0;j--){a=a+n[j];} a/nl }; console.log("100k for loop took " + (Date.now()-s) + "ms."); s=Date.now();for(i=0;i<1000000;i++){n=[1,2,3]; nl=n.length; a=0; for(j=nl-1;j>0;j--){a=a+n[j];} a/nl }; console.log("1M for loop took " + (Date.now()-s) + "ms."); s=Date.now();for(i=0;i<1000000;i++){ n=[1,2,3]; a=n.reduce((a,b) => (a+b)) / n.length }; console.log("1M reduce took " + (Date.now()-s) + "ms."); /* * RESULT on Chrome 51 * 100k reduce took 26ms. * 100k for loop took 35ms. * 10M for loop took 126ms. * 10M reduce took 209ms. */

我觉得我们可以

var k=elmt.reduce(function(a,b){return parseFloat(a+parseFloat(b));})
var avg=k/elmt.length; 
console.log(avg);

我使用了两次parseFloat,因为 当 1)你添加(a)9+b(“1”)数字,结果将是“91”,但我们想要加法。所以我使用了parseFloat

2)当添加(a)9+parseFloat(“1”)发生,虽然结果将是“10”,但它将是字符串,我们不想要的,所以我再次使用parseFloat。

我希望我讲清楚了。欢迎提出建议

这是你的一句台词:

var average = arr.reduce((sum,item,index,arr)=>index !== arr.length-1?sum+item:sum+item/arr.length,0)

只是为了好玩

让avg =[81、77、-88、195、6.8].reduce ((a, e, i) = >(*我+ e) / (i + 1)); console.log (avg)

在阅读了其他选项之后,我将尝试为未来的观众创建一个更简单的版本,详细说明现有的代码,而不是创建一个更优雅的代码。首先,您将数字声明为字符串。除了.parseInt,我们还可以做:

const numberConverter = elmt.map(Number);

map所做的就是“返回原始数组的副本”。但是我把它的值转换成数字。然后我们可以使用reduce方法(它也可以更简单,但我写的是易于阅读的版本,我也有2个平均方法)reduce方法所做的是,它有一个累加器,当它遍历数组并添加(在这种情况下)currentValue时,如果你向它添加值,它会变得越来越大。

var i;
const 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';

console.log(elmt);

const numberConverter = elmt.map(Number);

const sum = numberConverter.reduce((accumulator, currentValue) => {
  return accumulator + currentValue;
}, 0);

const average = numberConverter.reduce(
  (accumulator, currentvalue, index, numArray) => {
    return accumulator + currentvalue / numArray.length;
  },
  0
);

const average2 =
  numberConverter.reduce(
    (accumulator, currentValue) => accumulator + currentValue,
    0
  ) / numberConverter.length;

for (i = 9; i < 10; i++) {
  console.log(
    `The sum of all the elements is: ${sum}. <br> The average of all the elements is: ${average2}`
  );}