Created
January 15, 2012 23:50
-
-
Save masakielastic/1618066 to your computer and use it in GitHub Desktop.
JavaScript の reduce と reduceRight の練習
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 a = [1, 2, 3]; | |
function f(x, y) { | |
return 2 * x + y; | |
} | |
var b = a.reduce(f, 4); | |
var c = a.reduceRight(f, 4); | |
// 43 | |
console.log(b); | |
console.log(f(f(f(4, 1), 2), 3)); | |
console.log(2 * (2 * (2 * 4 + 1) + 2) + 3); | |
// 49 | |
console.log(c); | |
console.log(f(f(f(4, 3), 2), 1)); | |
console.log(2 * (2 * (2 * 4 + 3) + 2) + 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment