Skip to content

Instantly share code, notes, and snippets.

@piyonishi
Created May 19, 2015 08:19
Show Gist options
  • Save piyonishi/a48f2e21013f6e73431d to your computer and use it in GitHub Desktop.
Save piyonishi/a48f2e21013f6e73431d to your computer and use it in GitHub Desktop.
// 命令形
var total = 0,
num = 0,
ave,
i;
for (i = 1; i <= 10; i++) {
total += i;
num++;
}
ave = total / num; console.log(ave); // 5.5
// 宣言型
var add = function(a, b) {
return a + b;
};
var average = function(array) {
return array.reduce(add) / array.length;
};
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var ave = average(numbers);
console.log(ave); // 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment