The function displays anything, but not the arithmetic mean:

function arrayAvg(array) { var sum = 0; var arrLength = array.length; for (var i = 0; i < arrLength; i++) { sum =+ array[i]; } var arrAvg = sum / arrLength; return arrAvg; } var arr333 = []; arr333 = arr333.concat(67); arr333 = arr333.concat(85); arr333 = arr333.concat(51); arr333 = arr333.concat(76); var avg333 = arrayAvg(arr333); console.log(avg333); 

Closed due to the fact that off-topic participants Grundy , Regent , Cheg , br3t , user207618 3 Aug '17 at 4:51 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Grundy, Regent, Cheg, br3t, Community Spirit
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    Change sum =+ array[i]; sum += array[i]; and you will be the arithmetic mean. - Regent
  • four
    displays the last element divided by the length of the array: in this case 76/4 === 19 - Grundy

3 answers 3

The problem is here:

  for (var i = 0; i < arrLength; i++) { sum +=array[i]; } 

Just wrong with the assignment.

     function arrayAvg(array) { var sum = 0; var arrLength = array.length; for (var i = 0; i < arrLength; i++) { sum += +array[i]; } var arrAvg = sum / arrLength; return arrAvg; } var arr333 = arrayAvg([].concat(67, 85, 51, 76)); console.log(avg333); 
    • one
      Then you can simply var arr333 = [67, 85, 51, 76]; . And you should write in the answer where the author you find a mistake and how to fix it. - Regent
    • @Regent here is more a message towards concat, if you use it, then do not forget the options) - Artsiom
    • one
      Thanks for the syntax "twists"! :) I put it into service. - Vad Weber

    Let me offer the body of the function in one line, WITHOUT deterioration of readability:

     return arr.reduce((sum, i) => i + sum) / arr.length 

    IMPORTANT. The arrow function will NOT work in IE up to version 11 inclusive. If IE 11 is needed, simply replace (sum, i) => i + sum with function(sum, i) {return i + sum}

    • you have confused the parameters in some places: the first parameter is just the battery (sum in your case) and not i (index), by the way i , the element (el, elem) is also better, so that it is clear - Grundy
    • @ Oleg, switch function? : D This is called Lambda . - And
    • @Grundy fixed. I just in this case more like it. Even before i became (index) , it was customary to name variables by their type. i was an integer and later became just a numeric type. Having written i I kind of emphasized that the function is intended for working with numbers. Here the question of taste and perception of course. - Oleg
    • @Oleg, I have never seen i denote an integer by itself, since this is the commonly used name for a counter in a loop. And not everyone likes the Hungarian notation. - Grundy
    • @And developer.mozilla.org/en/docs/Web/JavaScript/Reference/ ... That's exactly what is called - Oleg