0

i am having issues (very lost) in making a code that me and my friend were given to create.

So I am suppose to compute and return the average of all values in a given array named customerBalance, the array holds the amount of "what customers owe my business" (I dont own a business) and each item in the array holds the "customers balance", i also have to use a for() to process the array and calculate the average and divide by customerBalance length, and finally return the average.

Here is my code so far

function average() {
    customerBalance
    for(i=0,i++)
      sum(customerBalance)
    total=sum/5

return average;

I know that this is COMPLETELY wrong, I am not sure on how i start typing the array, please don't be harsh I would really like to know how to do this.

Thank you and have a great day

artm
  • 8,554
  • 3
  • 26
  • 43
Mike W
  • 1
  • I just realalized that what i have so far did not indent and properly form when i copy and pasted it onto here, I am sorry that makes it more difficult to read. – Mike W Oct 23 '14 at 01:01
  • possible duplicate of [Array Sum and Average](http://stackoverflow.com/questions/10359907/array-sum-and-average) – rjdown Oct 23 '14 at 01:04

1 Answers1

0
function average(customerBalance) {
    if (customerBalance.length == 0) { // Prevent division by zero later
        return 0;
    }
    var total = 0;
    for (var i = 0; i < customerBalance.length; i++) {
        total += customerBalance[i];
    }
    var average = total/customerBalance.length;
    return average;
}

You have many problems:

  1. The parameter to a function goes in the parenthese after the function name, not the next line.
  2. Your for() syntax is all wrong. You need to put the initialization, repetition test, and increment separated by semicolons.
  3. There's no sum() function in Javascript. And even if there were, you would need to assign the result to a variable.
  4. When you calculate the average, you're putting it in total, but then you're returning average, which is the variable that contains the function, not the average you calculated.

Other recommendations:

  1. Don't hard-code the array size, use array.length to get it.
  2. Always put braces around the body of for, if, while, etc. even if they're just one line.
  3. Local variables should be declared with var.
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you very much Barmar, where you put //Prevent division by zero later, would the "//" go to the next line as a comment? Inserting it the line before return 0;? – Mike W Oct 23 '14 at 01:13
  • Everything after `//` on a line is a comment and is ignored. Do you know even the most basic thing about Javascript syntax? – Barmar Oct 23 '14 at 01:15
  • I know that the comment line is ignored and aware that (at least for the course im in) is required for every 3 lines of code, I was just wondering if that is necesarry there or after the return 0;. And no i am not to familiar with it at all very intrigued and think it's cool and would like to learn more – Mike W Oct 23 '14 at 01:17
  • It doesn't matter where you put the comment. It's totally ignored, it's just for the benefit of human readers. – Barmar Oct 23 '14 at 01:19
  • Still missing a local declaration of the `i` variable. – jfriend00 Oct 23 '14 at 01:49