-
-
Save Sensiblemnd/a2a487109acdd275be8842110a2b6853 to your computer and use it in GitHub Desktop.
group consecutive numbers in array , in Javascript.
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
/*TODO MAKE IT IN ES6 | |
var data=[4,5,6,9,10,14,15,20,21,22,23,24,25,30,31,34] | |
output | |
4-6,9-10,14-15,20-25,30-31,34 | |
*/ | |
var data=[4,5,6,9,10,14,15,20,21,22,23,24,25,30,31,34,36,37,94,95]; | |
var start=data[0]; | |
var temp=1; | |
var a=0; | |
cons(start,temp); | |
function cons(s,t){ | |
if(s+1==data[t]){ | |
s=data[t]; | |
t=t+1; | |
cons(s,t); | |
} | |
else{ | |
print(a,t-1); | |
} | |
} | |
function print(k,t){ | |
display(k,t); | |
t++; | |
a=t; | |
start=data[t]; | |
if(t<data.length){cons(start,t+1)} | |
} | |
function display(k,t){ | |
if(k!=t){ | |
console.log(data[k]+'-'+data[t]);} | |
else{ | |
console.log(data[k]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment