Created
August 2, 2012 09:40
-
-
Save joaomaia/3235910 to your computer and use it in GitHub Desktop.
Jazzychad's Array iteration question.
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 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