3 ms·
Here's my 10-minutes-of-thought JavaScript implementation. I was going to do Ruby until I realized how long it's been since I wrote any Ruby. I only got it dow
by prophetjohn 14y ago
Here's my 10-minutes-of-thought JavaScript implementation. I was going to do Ruby until I realized how long it's been since I wrote any Ruby.
I only got it down to 2 cases
function add1(arr, val, n) {
var m = 0, i;
if(n > 0) {
for(i = 0; i < arr.length && m < n; i++) {
if(arr[i] === val) {
arr[i]++;
m++;
}
}
} else {
for(i = arr.length; i >= 0 && (m !== n || !n); i--) {
if(arr[i] === val) {
arr[i]++;
m--;
}
}
}
console.log(arr);
}