Average of array by using a callback function in Javascript - Stack Overflow

admin2025-04-21  0

I'm having a problem to sum and find the average of an array. I keep getting average is 0. I cant really see what the problem could be :

var a = [10,20,30,40]
var sum = 0
var avg = 0
function sumUP(a) {
   for (var i=0; i<a.length; i++){
      sum += a[i];
      avg = (sum/a.length);
   }
}
function display(avg, callback) {
   document.write("Average of array is " + avg)
   callback(average);   
}
display(avg, sumUP);

thanks a lot in advance!

I'm having a problem to sum and find the average of an array. I keep getting average is 0. I cant really see what the problem could be :

var a = [10,20,30,40]
var sum = 0
var avg = 0
function sumUP(a) {
   for (var i=0; i<a.length; i++){
      sum += a[i];
      avg = (sum/a.length);
   }
}
function display(avg, callback) {
   document.write("Average of array is " + avg)
   callback(average);   
}
display(avg, sumUP);

thanks a lot in advance!

Share Improve this question edited Apr 23, 2018 at 4:57 A J 1,5416 gold badges29 silver badges46 bronze badges asked Apr 23, 2018 at 4:10 Chris A.Chris A. 357 bronze badges 1
  • 1 You are displaying the value of avg before calling the function that sets it (and there are issues with what you pass in the call). – RobG Commented Apr 23, 2018 at 4:21
Add a ment  | 

5 Answers 5

Reset to default 3

Instead of assigning to global variables, you should have sumUP return the calculated average so it can be used in your document.write. That's one of the purposes of functions - to encapsulate functionality and return values that can be used by other functions without unnecessary side-effects. You also need to calculate the average before you display it. (you were displaying it before you were calculating it, which of course leaves it displaying 0)

You should also take care not to use single-letter variable names, which are very hard to make sense of (not only for others reading your code, but also for you, when you e back to it later).

const array = [10, 20, 30, 40];
function sumUP(arr) {
  let sum = 0;
  for (var i = 0; i < arr.length; i++) {
    sum += arr[i];
  }
  const avg = (sum / arr.length);
  return avg;
}
function display(arr, callback) {
  const avg = callback(arr);
  document.write("Average of array is " + avg)
}
display(array, sumUP);

You should write code like this.

function sumUP(a) {
for (var i=0; i<a.length; i++){
sum += a[i];
}
avg = (sum/a.length);
}

take a look at : .reduce()

const arr = [10, 20, 30, 40]

const sum = arr.reduce((a,b) => a + b , 0)

const avg = sum / arr.length

function display(avg) {
   document.write("Average of array is " + avg)
}

display(avg);

You do not need to pass avg to the callback and display function at all:

var a = [10,20,30,40];
var sum = 0;
var avg = 0;
function sumUP() {
  for (var i=0; i<a.length; i++){
    sum += a[i];
    res = (sum/a.length);
  }
}
function display(callback) {
  callback();
  document.write("Average of array is: " + res);
}
display(sumUP);

Here is the fix for your code.

var a = [10,20,30,40]
var sum = 0
var avg = 0
function sumUP(a) {
   for (var i=0; i<a.length; i++){
      sum += a[i];
   }
   avg = (sum/a.length);
}
function display(callback) {
    callback(a);
    document.write("Average of array is " + avg);
}
display(sumUP);

The first problem in your code was you are calling the callback function after the document write. It should be called first before the writing the result because these method have your logic.

And the avg that you are calling is not the global variable that you declare but the variable that is being used by the function. I remove it in your parameter because it was already declared as global and no need to pass in the display function.

I also exlude avg = (sum/a.length); in the for loop because it can be puted once, and that is when the for loop is done or when you get the total sum of your numbers.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745177640a289002.html

最新回复(0)