3 ms·
Here's my version without using reverse or underscore. If coffeescript supported reverse loops or negative indices, this would be a lot cleaner. add1 = (ar
by cezary 14y ago
Here's my version without using reverse or underscore. If coffeescript supported reverse loops or negative indices, this would be a lot cleaner.
add1 = (arr, val, n)->
d = Math.abs(n||1)/(n||1)
n = if n is 0 then arr.length else Math.abs n
i = if d < 0 then arr.length - 1 else 0
end = if d < 0 then 0 else arr.length
while n and i isnt end
num = arr[i]
if num is val
n--
arr[i]++
i = i + d
arr