Skip to content

Instantly share code, notes, and snippets.

@40
Forked from tracycodes/gist:3233358
Created August 2, 2012 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 40/3240881 to your computer and use it in GitHub Desktop.
Save 40/3240881 to your computer and use it in GitHub Desktop.
Answer to a random interview question
function add1(array, val, n) {
var iter = 0,
end = array.length,
count = (n) ? n : -1,
multiple = 1;
if(n < 0) {
iter = array.length - 1;
end = 0;
multiple = -1;
}
while(iter !== end && count) {
if(array[iter] === val) {
array[iter]++;
count--;
}
iter += multiple;
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment