Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Javascript, ES6
- const checkIfCanGetOpinion = timeStamps => {
- var statistics = {
- threeDaysInRow: false,
- sessions: 0
- };
- // Create array of dates
- var datesArray = [];
- timeStamps.forEach(stamp => {
- const stampToUTC = stamp.replace(" ", "T") + "Z";
- const date = new Date(stampToUTC);
- datesArray.push(date);
- });
- // Transform dates into number of days
- const dayInMiliseconds = 24 * 60 * 60 * 1000;
- var datesToDays = datesArray.map(key => {
- return Date.parse(key) / dayInMiliseconds;
- });
- // above datesToDays array looks more or less like this: [17235.123123, 17235.123123, 17236.123123, 17236.123123, 17237.123123, 17237.123123]
- const firstDay = datesToDays[0];
- var daysInRow = datesToDays.map(day => {
- return Math.floor(day) - Math.floor(firstDay);
- });
- // remove duplicates
- daysInRow = daysInRow.filter(function(elem, pos, arr) {
- return arr.indexOf(elem) == pos;
- });
- // daysInRow array should be [0, 1, 2]
- if (daysInRow.join(",") === "0,1,2") {
- statistics.threeDaysInRow = true;
- } else {
- return false;
- }
- var sessionTimesInMiliseconds = datesArray.map(date => {
- return Date.parse(date);
- });
- // Substract the time of the previous session from this session to get time gaps between sessions
- var timeGapsBetweenSessions = [];
- const minGapLengthInMiliseconds = 30 * 60 * 1000;
- for (let i = 0; i < sessionTimesInMiliseconds.length - 1; i++) {
- var gap = sessionTimesInMiliseconds[i + 1] - sessionTimesInMiliseconds[i];
- timeGapsBetweenSessions.push(gap);
- }
- statistics.sessions += 1; // first session is already checked
- timeGapsBetweenSessions.forEach(gap => {
- if (gap >= minGapLengthInMiliseconds) statistics.sessions += 1;
- });
- if (statistics.threeDaysInRow === true && statistics.sessions >= 6) {
- return true;
- } else {
- return false;
- }
- };
Add Comment
Please, Sign In to add comment