Created
June 14, 2020 03:00
-
-
Save jebai0521/64a7f2254573acb40c91061a08a318f2 to your computer and use it in GitHub Desktop.
week days
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 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