0

I have an issue with getting the sum of two arrays and combining their averages while rounding off.

I don't want to hardcode but rather pass two random arrays.
so here is the code but it keeps returning NaN

function sumAverage(arr) {
  var result = 0;
  // Your code here
  // set an array
  arr = [];
  a = [];
  b = [];
  arr[0] = a;
  arr[1] = b;

  var sum = 0;

  // compute sum of elements in the array
  for (var j = 0; j < a.length; j++) {
    sum += a[j];
  }

  // get the  average of elements in the array
  var total = 0;
  total += sum / a.length;


  var add = 0;

  for (var i = 0; i < b.length; i++)
    add += b[i];

  var math = 0;
  math += add / b.length;

  result += math + total;

  Math.round(result);

  return result;
}
console.log(sumAverage([
  [2, 3, 4, 5],
  [6, 7, 8, 9]
]));

3
  • 1
    Hi @Martin have you done any of your own debugging here? Narrowed down the issue to a specific area of your function? Commented Jun 7, 2017 at 13:22
  • 2
    You reset arr to an empty array at the beginning of your function. Don't do that. Commented Jun 7, 2017 at 13:23
  • 3
    b.length is always 0. Browsers have great debugging tools these days. Set a breakpoint, step through the code and inspect the values of the variables. Commented Jun 7, 2017 at 13:25

7 Answers 7

2

If you wanted to do it a bit more functionally, you could do something like this:

function sumAverage(arrays) {
  const average = arrays.reduce((acc, arr) => {
    const total = arr.reduce((total, num) => total += num, 0);
    return acc += total / arr.length;
  }, 0);
  
  return Math.round(average);
}

console.log('sum average:', sumAverage([[1,2,3], [4,5,6]]));

Sign up to request clarification or add additional context in comments.

Comments

0

Just try this method..this kind of issues sometimes occured for me.

For example

 var total = 0;
 total = total + sum / a.length;

And every concat use this method..

1 Comment

So you're saying to use total = total + 1 instead of total += 1? Am I understanding correctly? I don't see much difference between your suggestion and what the OP is already using... (not my DV)
0

Because you are assigning the value [] with the same name as the argument? This works, see jFiddle

function sumAverage(arr) {
  var result = 0;

  //arr = [];
  //a = [];
  //b = [];
  a = arr[0];
  b = arr[1];

  var sum = 0;

  // compute sum of elements in the array
  for(var j = 0; j < a.length; j++ ){
    sum += a[j] ;
  }

// get the  average of elements in the array
var total = 0;
total += sum / a.length;

var add = 0;

for(var i = 0; i < b.length; i++)
    add += b[i];

  var math = 0;
  math += add / b.length;
  result += math + total;

Math.round(result);
  return result;
}
document.write(sumAverage([[2,3,4,5], [6,7,8,9]]));

Comments

0

As said in comments, you reset your arguments...

Use the variable "arguments" for dynamic function parameters.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

Comments

0

I suggest to use two nested loops, one for the outer array and one for the inner arrays. Then sum values, calculate the average and add averages.

function sumAverage(array) {
    var result = 0,
        sum,
        i, j;

    for (i = 0; i < array.length; i++) {
        sum = 0;
        for (j = 0; j < array[i].length; j++) {
            sum += array[i][j];
        }
        result += Math.round(sum / array[i].length);
    }
    return result;
}

console.log(sumAverage([[2, 3, 4, 5], [6, 7, 8, 9]])); // 12

Comments

0

The problem is that you are emptying arr by saying arr = [].

Later, you are iterating over a which is empty too. Again when you say total += sum / a.length;, sum is 0 and a.length is 0 so 0/0 becomes NaN. Similarly for math. Adding Nan to NaN is again NaN and that's what you get.

Solution is to not empty passed arr and modify your code like below:

function sumAverage(arr) {
  var result = 0;
  // Your code here
  // set an array
  //arr = [];
  //a = [];
  //b = [];
  a = arr[0];
  b = arr[1];

  var sum = 0;

  // compute sum of elements in the array
  for (var j = 0; j < a.length; j++) {
    sum += a[j];
  }

  // get the  average of elements in the array
  var total = 0;
  total = sum / a.length;


  var add = 0;

  for (var i = 0; i < b.length; i++)
    add += b[i];

  var math = 0;
  math = add / b.length;

  result = math + total;

  result = Math.round(result);

  return result;
}
console.log(sumAverage([
  [2, 3, 4, 5],
  [6, 7, 8, 9]
]));

Comments

0

Basically I see a mistake here: arr[0] = a; arr[1] = b;

That should be a= arr[0]; b= arr[1];

and then remove: arr = [];


I suggest you write your function like this:

function sum(arr) {
var arr1 = arr[0]
var sum1 = 0;
arr1.map(function(e){sum1+=e});
var arr2 = arr[1]
var sum2 = 0;
arr2.map(function(e){sum2+=e});
return Math.round(sum1/arr1.length + sum2/arr2.length);
}

7 Comments

This actually works, i am glad it wasn't a major issue as i thought.
Happy to help you Martin, don't forget to mark it as solved
@martin You wanted sum of averages right? Given your data the answer should be 11, which is sum of 3.5 and 7.5 isn't it?
@PankajShukla: You're right, let me update my suggestion
@R.Saban You are passing two arrays to the function. OP is passing just one nested array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.