Created
May 19, 2015 08:19
-
-
Save piyonishi/a48f2e21013f6e73431d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 命令形 | |
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