-1

I'm trying to make a function where there is a for loop so I can get the average of the numbers inside an array and found this solution to adding them in stack overflow, which contains the line

z += array[i];

The thing is i don't know what it does, can anybody explain?

var x = [80, 82, 84, 92];

function PromedioArrays(array) {
  var z = 0;

  for (var i = 0; i < array.length; i++) {
    z += array[i];

    console.log(array[i]);

    console.log(z);
  }

  console.log(z / array.length);
}

PromedioArrays(x);
terrymorse
  • 6,771
  • 1
  • 21
  • 27
  • `+=` is an *assignment operator.* Read about [Assignment Operators in the MDN docs.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators) – Robert Harvey May 09 '20 at 00:16

1 Answers1

0

That's an Addition Assignment Operator: it will sum the left and right operator and assign it to the left operator.

Example:

let x = 0;
let y = 10;

x += y;

console.log(x, y); // outputs 10, 10

In the code you gave, it's summing the values of the array and storing into z variable.

Elias Soares
  • 9,884
  • 4
  • 29
  • 59