Created
August 30, 2016 23:16
-
-
Save dthtvwls/e4e0d10534f6c8b535cba151731a34df to your computer and use it in GitHub Desktop.
sessionize
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 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