Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Created January 15, 2012 23:50
Show Gist options
  • Save masakielastic/1618066 to your computer and use it in GitHub Desktop.
Save masakielastic/1618066 to your computer and use it in GitHub Desktop.
JavaScript の reduce と reduceRight の練習
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