Skip to content

Instantly share code, notes, and snippets.

@sebastien-p
Forked from 140bytes/LICENSE.txt
Created June 25, 2011 21:02
Show Gist options
  • Save sebastien-p/1046891 to your computer and use it in GitHub Desktop.
Save sebastien-p/1046891 to your computer and use it in GitHub Desktop.
Array.prototype.reduceRight
// 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
}
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}
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.
{
"name": "Array.prototype.reduceRight",
"description": "ES5 Array.prototype.reduceRight polyfill",
"keywords": [
"ES5",
"array",
"reduceRight",
"polyfill"
]
}
<!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>
@Kambfhase
Copy link

Your code fails for the initialValue -1.

@sebastien-p
Copy link
Author

@Kambfhase : thanks. This code is not the right way to do the trick, I'm currently working on a major rewrite.

@sebastien-p
Copy link
Author

Please, help me, I'm too tired to keep trying for today

@jed
Copy link

jed commented Jun 27, 2011

i wonder if a recursive solution would be shorter?

@sebastien-p
Copy link
Author

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.

@bytespider
Copy link

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