Created
November 15, 2012 16:26
-
-
Save jgable/4079522 to your computer and use it in GitHub Desktop.
Arrays Equal in CoffeeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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