Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function(outer_result){
- var result_US = {bens:{}, nof_counties:0, votes:0};
- var cands = [1, 0.666666, 0.6, 0.55, 0.5, 0.45, 0.4, 0.333333];
- function count(counties){
- var bens = {};
- var nof_counties = 0;
- counties.forEach(county => {
- var votes = 0;
- county.candidates.forEach(cand => {
- countByCand(bens, cand);
- countByCand(result_US.bens, cand);
- votes += cand.voteNum;
- })
- make_pseudo_cands(bens, votes);
- nof_counties++;
- result_US.votes += votes;
- });
- result_US.nof_counties += nof_counties;
- var props = make_props(bens, nof_counties);
- return {bens, nof_counties, props, diffs:make_diffs(props)};
- }
- function make_pseudo_cands(bens, votes){
- for (var i=0;i<cands.length;i++) countByCand(bens, {lastName:'C'+(cands[i]*100).toString().split('.')[0], voteStr:(votes*cands[i]).toString()});
- }
- function countByCand(bens, cand){
- if (cand.voteNum===0) return;
- if (!bens[cand.lastName]) bens[cand.lastName] = {};
- bens[cand.lastName][cand.voteStr[0]] = (bens[cand.lastName][cand.voteStr[0]]||0) +1;
- }
- var props_theoretical = {};
- for (var i=1;i<10;i++) props_theoretical[i] = Math.log10(i+1)-Math.log10(i);
- function make_props(bens, nof_counties){
- var props = {};
- for (var c in bens){
- props[c] = {};
- for (var i in bens[c]) props[c][i] = bens[c][i]/nof_counties;
- }
- return props;
- }
- function make_diffs(props){
- var simple= {};
- var weighted = {};
- for (var c in props){
- simple[c] = {avg:0};
- weighted[c] = {avg:0};
- var num = 0;
- for (var i in props[c]) {
- simple[c][i] = Math.abs(props[c][i]-props_theoretical[i]);
- simple[c].avg += simple[c][i];
- weighted[c][i] = simple[c][i]/props_theoretical[i];
- weighted[c].avg += weighted[c][i];
- num++;
- }
- simple[c].avg /= num;
- weighted[c].avg /= num;
- }
- return {simple, weighted};
- }
- // var states = ["IL"];
- // var states = ["WI"];
- // var states = ["MI", "WI"];
- // var states = ['GA', 'NC', 'PA', 'NV', 'AZ'];
- var states = ['VT', 'SC', 'IN', 'KY', 'GA', 'VA', 'WV', 'NC', 'OH', 'MA', 'AL', 'FL', 'DE', 'MD', 'DC', 'NH', 'MS', 'RI', 'ME', 'NJ', 'OK', 'PA', 'IL', 'CT', 'MO', 'TN', 'AR', 'NY', 'ND', 'CO', 'MI', 'KS', 'NE', 'MN', 'TX', 'NM', 'WI', 'WY', 'LA', 'AZ', 'SD', 'UT', 'MT', 'NV', 'IA', 'ID', 'OR', 'CA', 'WA', 'HI', 'AK'];
- var jobs = [];
- for (var i=0;i<states.length;i++) {
- var url = 'https://politics-elex-results.data.api.cnn.io/results/view/2020-county-races-PG-'+states[i]+'.json';
- jobs.push(fetch(url)
- .then(res => res.json())
- .then(data => {
- var state = data[0].stateAbbreviation;
- result_US[state] = count(data);
- console.log(result_US[state]);
- if (outer_result) outer_result.res = result_US;
- }));
- }
- Promise.all(jobs).then(
- _ => {
- make_pseudo_cands(result_US.bens, result_US.votes);
- result_US.props = make_props(result_US.bens, result_US.nof_counties);
- result_US.diffs = make_diffs(result_US.props);
- console.log(result_US);
- }
- );
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement