What is wrong with this code? (average coding)

Hello!
I’m trying to make a code to get the average of an array, someone can tell me why this doesn’t work?
thanks in advance:

function average(array) {
var i = array.length;
while (i>=0) {
var suma = array[i-1] + array[i–];
}
return suma/i;
}

you create the variable inside the loop, that means you will just get the sum of two elements

if you want the sum of all elements you need to do in a different way

This doesn’t work either…
I think is a syntax error but I’m not sure how to write it. Any idea?

function average(array) {
var i = array.length;
var suma = while(i>=0) {
array[i] + array[i–];
}
return suma/i;
}

a loop doesn’t return anything, it just repeats again and again a piece of code

you can’t assign a loop to a variable