Write a function in javascript that calculates the average element in a non-empty list.
Solution
function average(array)
{
var sum = 0;
for( var i = 0; i < array.length; i++ ){
sum += parseInt( array[i]); //don't forget to add the base
}
var avg = sum/array.length;
window.alert( "The average of all the elements is: " + avg);
}

Write a function in javascript that calculates the average element i.pdf

  • 1.
    Write a functionin javascript that calculates the average element in a non-empty list. Solution function average(array) { var sum = 0; for( var i = 0; i < array.length; i++ ){ sum += parseInt( array[i]); //don't forget to add the base } var avg = sum/array.length; window.alert( "The average of all the elements is: " + avg); }