News for hackers. Stuff that matters.
Re:Array Iteration Interview Problem by cezary on Thursday August 02, 2012 @02:26AM (#4327580)
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

There are no comments on this comment yet.