Skip to content

Instantly share code, notes, and snippets.

@RyanGough
Created October 5, 2013 00:51
Show Gist options
  • Save RyanGough/6835152 to your computer and use it in GitHub Desktop.
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"
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
})
}
@RyanGough
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment