Skip to content

Instantly share code, notes, and snippets.

@joaomaia
Created August 2, 2012 09:40
Show Gist options
  • Save joaomaia/3235910 to your computer and use it in GitHub Desktop.
Save joaomaia/3235910 to your computer and use it in GitHub Desktop.
Jazzychad's Array iteration question.
var add1 = function (arr, val, n) {
//Callback function for the reduce method.
var cb = function(remaining, cur, i, arr) {
if(cur === val && remaining > 0) {
arr[i]++;
--remaining;
}
return remaining;
}
if(n === Math.abs(n)) {
arr.reduce(cb, n || arr.length);
} else {
arr.reduceRight(cb, (-n));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment