Skip to content

Instantly share code, notes, and snippets.

@jebai0521
Created June 14, 2020 03:00
Show Gist options
  • Save jebai0521/64a7f2254573acb40c91061a08a318f2 to your computer and use it in GitHub Desktop.
Save jebai0521/64a7f2254573acb40c91061a08a318f2 to your computer and use it in GitHub Desktop.
week days
function days(ds) {
const results = [];
for (let i = 0; i < ds.length;) {
// if (i === 0)results.push(days[i]);
console.log(i);
const s = i;
let t = i;
while (ds[t + 1] && ds[t] && ds[t + 1] - ds[t] === 1) {
t++;
}
i = t;
i++;
// results.push(s);
if (results.length > 0) results.push(',');
if (t === s) results.push(ds[s]);
else if (t - s === 1) results.push(ds[s], '-', ds[t]);
else if (t - s > 1) results.push(ds[s], ' - ', ds[t]);
}
return results.join('');
}
console.log(days([ 1, 2, 4, 5, 6 ]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment