News for hackers. Stuff that matters.
Re:Array Iteration Interview Problem by jazzychad on Wednesday August 01, 2012 @09:11PM (#4326679)
another double reverse... I guess coffeescript really does want to be ruby :)
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
Re:Array Iteration Interview Problem by azylman on Wednesday August 01, 2012 @09:24PM (#4326721)
Here's my cheating version that uses a third-party library:

    add1_cheating = (arr, val, n) ->
      n = arr.length if n is 0
      start = if n < 0 then arr.length else 0
      end = if n < 0 then 0 else arr.length
      step = n/(Math.abs n)
      n = Math.abs n
      count = 0
      for i in _.range start, end, step
        break if count > n
        arr[i]++ if arr[i] is val
        count++
      return arr
Re:Array Iteration Interview Problem by azylman on Wednesday August 01, 2012 @09:13PM (#4326684)
There was only two ways I could think of to do this without the double reverse.

One would introduce more complex branches (looping backwards in CoffeeScript ain't easy) and the other would be using Underscore.js's range function to do the same thing - that solution would be basically identical to this, except without the reverses, only I'd consider that cheating.

Re:Array Iteration Interview Problem by radagaisus on Wednesday August 01, 2012 @09:34PM (#4326744)
People are just lazy since it usually doesn't matter.

array[i]++ for i in [-1..-array.length]