Skip to main content
Code Review

Return to Question

edited title
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

How do I make this average JavaScript function better/more elegant?to calculate the average of an array

Notice removed Reward existing answer by RubberDuck
Bounty Ended with Zirak's answer chosen by RubberDuck
Notice added Reward existing answer by RubberDuck
Bounty Started worth 100 reputation by RubberDuck
deleted 110 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I have this function that calculates average. I don't want it to look like this. I am hoping for a less cliche average function. Because because I see that most people's look like this. Can anyone help?

function mean(array) {
 var num=0;
 for (var i=1;i<=array.length;i++) num=num+array[i];
 var divide=num/array.length;
 alert(divide);
}​

Can anyone change this up to be better than before? It doesn't have to be more efficient but just look better. Thanks! (By the way I'm new to JavaScript so if you can explain your code that'd be great).

I have this function that calculates average. I don't want it to look like this. I am hoping for a less cliche average function. Because I see that most people's look like this. Can anyone help?

function mean(array) {
 var num=0;
 for (var i=1;i<=array.length;i++) num=num+array[i];
 var divide=num/array.length;
 alert(divide);
}​

Can anyone change this up to be better than before? It doesn't have to be more efficient but just look better. Thanks! (By the way I'm new to JavaScript so if you can explain your code that'd be great).

I have this function that calculates average. I don't want it to look like this. I am hoping for a less cliche average function because I see that most people's look like this.

function mean(array) {
 var num=0;
 for (var i=1;i<=array.length;i++) num=num+array[i];
 var divide=num/array.length;
 alert(divide);
}​

Can anyone change this up to be better than before? It doesn't have to be more efficient but just look better.

Tweeted twitter.com/#!/StackCodeReview/status/230032906414874625
Source Link
user6607
  • 377
  • 2
  • 3
  • 8

How do I make this average function better/more elegant?

I have this function that calculates average. I don't want it to look like this. I am hoping for a less cliche average function. Because I see that most people's look like this. Can anyone help?

function mean(array) {
 var num=0;
 for (var i=1;i<=array.length;i++) num=num+array[i];
 var divide=num/array.length;
 alert(divide);
}​

Can anyone change this up to be better than before? It doesn't have to be more efficient but just look better. Thanks! (By the way I'm new to JavaScript so if you can explain your code that'd be great).

default

AltStyle によって変換されたページ (->オリジナル) /