Skip to content

Instantly share code, notes, and snippets.

@pavsidhu
Last active December 27, 2016 16: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 pavsidhu/8a3b04bc7f16ba6a2eb38f6803254fda to your computer and use it in GitHub Desktop.
Save pavsidhu/8a3b04bc7f16ba6a2eb38f6803254fda to your computer and use it in GitHub Desktop.
Study Streak
// Values for dates are received from the server
// This is sorted in most recent date first to least to recent last.
dates = [];
// Go through each date
for (i=0; i <= dates.length-1; i++) {
var date = date[i]; // Get the UTC date
var localDate = moment.utc(date).local().format('DDMMYY'); // Covert it to a local date
// If the date is the first in the list i.e. the last test recordeed
if (i === 0) {
var today = moment().format('DDMMYY');
var yesterday = moment().format('DDMMYY');
// Check if it was logged today or yesterday
if (localDate === today) ||
localDate === yesterday) {
streak += 1;
}
}
else { // If not the first date in the list
// Get the previous date in the list
var previousDate = moment.utc(dates[i-1]).local().format('DDMMYY');
var previousDateYesterday = moment.utc(dates[i-1]).local().subtract(1, 'days').format('DDMMYY');
// Make sure that the dates are not the same, we don't want to add to the streak if the dates are from the same day
// as the streak is counted daily.
if (localDate !== previousDate) {
// If the date is the same as the date before it subtracted by one day then add to streak
if (localDate === previousDateYesterday) {
streak += 1;
}
else {
// If not then the streak is no longer valid.
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment