Created
October 5, 2013 00:51
-
-
Save RyanGough/6835152 to your computer and use it in GitHub Desktop.
convert array of numbers into string with ranges i.e [1,2,3,4,6,8,9] => "1-4,6,8-9"
This file contains 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
function play (a) { | |
return a.reduce(function(c,t,i) { | |
if (t-a[i-1]<2) { | |
c=(c+'').replace(/-.$/, '') | |
c+='-' | |
} else c+=',' | |
return c+t | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
playing with this problem for js golf, i.e solve problem in minimum number of characters
wish i didn't need 2 returns using this approach, and having to force the accumulator to be a string is annoying too - anyone see any way to shave off a few characters