-
-
Save sebastien-p/1046891 to your computer and use it in GitHub Desktop.
Array.prototype.reduceRight
This file contains 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
// 109 bytes, 153 including 'Array.prototype.reduceRight=[].reduceRight||' ... | |
function ( | |
a, // callback | |
b // initialValue and result | |
){ | |
for ( | |
var c = this, // 'this' shortcut | |
d = c.length + 1, // iterator | |
e; // 'undefined' and flag for trailing "holes" in 'c' | |
d--; | |
) | |
b = d in c ? a(b, c[d], d, e = c) : e || b !== e ? b : c[d -= d - 1 in c]; | |
return b | |
} |
This file contains 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
function(a,b){for(var c=this,d=c.length+1,e;d--;)b=d in c?a(b,c[d],d,e=c):e||b!==e?b:c[d-=d-1 in c];return b} |
This file contains 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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Sebastien P. https://twitter.com/#!/_sebastienp | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains 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
{ | |
"name": "Array.prototype.reduceRight", | |
"description": "ES5 Array.prototype.reduceRight polyfill", | |
"keywords": [ | |
"ES5", | |
"array", | |
"reduceRight", | |
"polyfill" | |
] | |
} |
This file contains 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
<!DOCTYPE html> | |
<title>Foo</title> | |
<div>Expected value: <b>16</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
Array.prototype.reduceRight=function(a,b){for(var c=this,d=c.length+1,e;d--;)b=d in c?a(b,c[d],d,e=c):e||b!==e?b:c[d-=d-1 in c];return b} | |
document.getElementById( "ret" ).innerHTML = [,,,1,2,,3,,,,,,].reduceRight(function(a,b){return a+b},10) | |
</script> |
Please, help me, I'm too tired to keep trying for today
i wonder if a recursive solution would be shorter?
I'm still working on it, please someone take a look at the current version and help me, I know we can do this ! Thanks.
My current version http://jsfiddle.net/vmFbp/2/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Kambfhase : thanks. This code is not the right way to do the trick, I'm currently working on a major rewrite.