Last active
July 12, 2016 17:27
-
-
Save brianloveswords/5886ac14c46aa6d9dd14855753ace9fc to your computer and use it in GitHub Desktop.
Script to use on http://www.theguardian.com/us-news/ng-interactive/2015/jun/01/the-counted-police-killings-us-database# to find out the longest time between killings.
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
// 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