Skip to content

Instantly share code, notes, and snippets.

@jgable
Created November 15, 2012 16:26
Show Gist options
  • Save jgable/4079522 to your computer and use it in GitHub Desktop.
Save jgable/4079522 to your computer and use it in GitHub Desktop.
Arrays Equal in CoffeeScript
arraysEqual = (nums1, nums2) ->
# Check the length
return false unless nums1.length == nums2.length
# Sort the arrays
nums1.sort()
nums2.sort()
# A helper for checking indices are same
sameNums = (idx) -> nums1[idx] == nums2[idx]
for num, i in nums1
# If we get a different number, return false
return false unless sameNums i
# All numbers are the same
return true
# Some tests
tests = [
[[1,2,3], [1,2,3]],
[[1,2,3], [1,2,3,4]],
[[1,2,3], [1,2,3,3]],
[[3,2,1], [1,2,3]],
]
# Output the test results
console.log "Test#{testNum}:", arraysEqual.apply(this, test) for test, testNum in tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment