Joined September 2014
·

seberius

Reno, NV
·
·

Nice, simple solution that can be easily adapted for lessThan and equals. Depending on your use case, you may want to add a check for array length, as

coffee> [0, 9].greaterThan [0, 8, 1]
true

Assuming you wanted to consider longer arrays to be greater, the following modification should work.

Array::greaterThan = (otherArray) ->
  thisLen = this.length
  otherLen = otherArray.length

  if thisLen isnt otherLen
    return thisLen > otherLen

  for element, index in @ when element isnt otherArray[index]
    return element > otherArray[index]
  false
Achievements
1 Karma
0 Total ProTip Views