Skip to content

Instantly share code, notes, and snippets.

@dthtvwls
Created August 30, 2016 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dthtvwls/e4e0d10534f6c8b535cba151731a34df to your computer and use it in GitHub Desktop.
Save dthtvwls/e4e0d10534f6c8b535cba151731a34df to your computer and use it in GitHub Desktop.
sessionize
function sessionize(nums, threshold) {
threshold = threshold || 3;
var i = 0, j = 0, sessions = [];
while (j < nums.length - 1) {
if (nums[j + 1] - nums[j] > threshold) {
sessions.push([nums[i], nums[j]]);
i = ++j;
} else {
++j;
}
}
sessions.push([nums[i], nums[j]]);
return sessions;
}
console.log(sessionize([1,2,3,17,18,19,20]));
console.log(sessionize([1,3,17,18,20]));
console.log(sessionize([1,17,30]));
console.log(sessionize([1,3,5,8,9,12,14,16,19,20,22,24,27,28,29,32]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment