Skip to content

Instantly share code, notes, and snippets.

@brianloveswords
Last active July 12, 2016 17:27
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 brianloveswords/5886ac14c46aa6d9dd14855753ace9fc to your computer and use it in GitHub Desktop.
Save brianloveswords/5886ac14c46aa6d9dd14855753ace9fc to your computer and use it in GitHub Desktop.
// The Counted uses lazy loading so make sure to scroll slowly down
// the page to the bottom before trying to run this or your count will
// be off.
let elements = Array.from(document.querySelectorAll('.record-inner .date'));
let dates = elements.map(e => new Date(e.innerHTML)).reverse();
let initialState = {
previousDate: dates.shift(),
maxDifference: 0,
count: 0,
dateRanges: [],
};
// Find out the max time between police shootings and keep track of ties.
let max = dates.reduce((state, currentDate) => {
let currentDifference = currentDate - state.previousDate;
if (currentDifference > state.maxDifference) {
state.count = 1;
state.maxDifference = currentDifference;
state.dateRanges = [ [state.previousDate, currentDate] ];
} else if (currentDifference == state.maxDifference) {
state.count += 1;
state.dateRanges.push([state.previousDate, currentDate]);
}
state.previousDate = currentDate;
return state;
}, initialState);
console.log(max);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment