-5

Here is what i have to start with, I'm learning javascript so please be as simplified as possible with your response. I understand if you have to be more detailed, please do so. Instructions are:

      /*
      create a function named 'avgNumbers'
       - accept 1 parameter into the function that will be an array of unlimited numbers
       - find the average of all the numbers
       - return the average from the function
       - console.log the answer outside of the function
     */

       //Code start here

       console.log("1. avg of an array of numbers");
       var avgNumbers = function(arr){

       }

      console.log('avg number = ', avgNumbers([1,2,3,4,5]));
  • Stack overflow is not a code writing service. Make an attempt first. – zero298 Feb 11 '14 at 19:59
  • 2
    What exactly do you mean by "unlimited"? What have you tried - your `avgNumbers` function is completely empty? Please show us your approaches. – Bergi Feb 11 '14 at 19:59
  • 1
    Function that takes in an array, add up all the items in the array and divide by the number of items in the array, poop answer out. Seems pretty straightforward enough. What's the issue? – kei Feb 11 '14 at 20:02
  • sorry guys, I'm just doing hw assignment and having trouble seeing the code at the moment. My apologize if this has been incorrectly posted. Im looking for help in getting started with writing this code. – simpleWebGuy Feb 11 '14 at 20:04
  • @kei thanks for your insight i will try now and add to above code – simpleWebGuy Feb 11 '14 at 20:05

5 Answers5

3

I'm not sure how much help you want. You won't learning anything if we simply give you the answer, so I'll give you some ideas, but no answer:

To compute an average of n numbers you can add them all and divide by n. For example, the average of the 3 numbers 1,1, and 7 is: (1 + 1 + 7)/3 = 3.

You want to do the same thing with your array, but n = array.length;.

So you should use a loop to compute the sum. Then after your loop divide by array.length and return the result.

Paul
  • 139,544
  • 27
  • 275
  • 264
2

You can use reduce to sum the values.

The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) has to reduce it to a single value

Then divide the sum by the number of elements in the array:

var avgNumbers = function(arr){
  return arr.reduce(function(a, b){ return a + b; }, 0) / arr.length;
}

http://jsfiddle.net/8v6Jg/

CD..
  • 72,281
  • 25
  • 154
  • 163
1

FIDDLE

Just loop all the element and add them, then divide by the number of element:

var avgNumbers = function (arr) {
     var total = 0;
     for (var i = 0; i < arr.length; i++) {
         total += arr[i];
     }
     return total / arr.length;
}
Shryme
  • 1,572
  • 1
  • 14
  • 22
1

try this:

console.log("1. avg of an array of numbers");
   var avgNumbers = function(arr){
        var sum = 0;
        for(var i =0;i<arr.length;i++) {
            sum+=arr[i];
        }
        return sum/arr.length;
   }

  console.log('avg number = ', avgNumbers([1,2,3,4,5]));
neo112
  • 1,703
  • 2
  • 17
  • 39
0

You can use arguments, its an array-like object corresponding to the arguments passed to a function.

var avg = function () {
    var sum =0;
    for (var x = 0; x < arguments.length; x++) {
        sum = sum + arguments[x]; //or Sum += scores[x];
    }

    return sum / arguments.length;
}
alert(avg(1, 2, 2))

DEMO

Satpal
  • 132,252
  • 13
  • 159
  • 168
  • 1
    The comment above the function in the OP specifically says that it should take in one argument that is an array, not multiple arguments. Still I agree that this would be a better way to do it, since it's more consistent with functions like `Math.max`, so +1 – Paul Feb 11 '14 at 20:05
  • 1
    @Paulpro, Anyways I never followed specification – Satpal Feb 11 '14 at 20:09