-1

I have a function which takes the average of all the elements in an array. However, I am getting an unexpected identifier error.

function calculateAverage(anyArray) {
    var sum = 0;
    for (int i = 0; i < anyArray.length; i++) {
        sum += anyArray[i];
    }
    sum = sum / anyArray.length;
}

I have counted my braces and parentheses and the error seems to be happening only when the for loop is included, as when I removed it the error does not occur.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
brld
  • 188
  • 2
  • 16

1 Answers1

4

In your for loop, you are using int, which is not a keyword in javascript, hence the error "Unexpected identifier". You should use var, or let.

hackerrdave
  • 6,486
  • 1
  • 25
  • 29