Advertisement
Guest User

Untitled

a guest
Nov 16th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(outer_result){
  2.   var result_US = {bens:{}, nof_counties:0, votes:0};
  3.   var cands = [1, 0.666666, 0.6, 0.55, 0.5, 0.45, 0.4, 0.333333];
  4.   function count(counties){
  5.     var bens = {};
  6.     var nof_counties = 0;
  7.     counties.forEach(county => {
  8.       var votes = 0;
  9.       county.candidates.forEach(cand => {
  10.         countByCand(bens, cand);
  11.         countByCand(result_US.bens, cand);
  12.         votes += cand.voteNum;
  13.       })
  14.       make_pseudo_cands(bens, votes);
  15.       nof_counties++;
  16.       result_US.votes += votes;
  17.     });
  18.     result_US.nof_counties += nof_counties;
  19.     var props = make_props(bens, nof_counties);
  20.     return {bens, nof_counties, props, diffs:make_diffs(props)};
  21.   }
  22.   function make_pseudo_cands(bens, votes){
  23.     for (var i=0;i<cands.length;i++) countByCand(bens, {lastName:'C'+(cands[i]*100).toString().split('.')[0], voteStr:(votes*cands[i]).toString()});
  24.   }
  25.   function countByCand(bens, cand){
  26.     if (cand.voteNum===0) return;
  27.     if (!bens[cand.lastName]) bens[cand.lastName] = {};
  28.     bens[cand.lastName][cand.voteStr[0]] = (bens[cand.lastName][cand.voteStr[0]]||0) +1;
  29.   }
  30.   var props_theoretical = {};
  31.   for (var i=1;i<10;i++) props_theoretical[i] = Math.log10(i+1)-Math.log10(i);
  32.   function make_props(bens, nof_counties){
  33.     var props = {};
  34.     for (var c in bens){
  35.       props[c] = {};
  36.       for (var i in bens[c]) props[c][i] = bens[c][i]/nof_counties;
  37.     }
  38.     return props;
  39.   }
  40.   function make_diffs(props){
  41.     var simple= {};
  42.     var weighted = {};
  43.     for (var c in props){
  44.       simple[c] = {avg:0};
  45.       weighted[c] = {avg:0};
  46.       var num = 0;
  47.       for (var i in props[c]) {
  48.         simple[c][i] = Math.abs(props[c][i]-props_theoretical[i]);
  49.         simple[c].avg += simple[c][i];
  50.         weighted[c][i] = simple[c][i]/props_theoretical[i];
  51.         weighted[c].avg += weighted[c][i];
  52.         num++;
  53.       }
  54.       simple[c].avg /= num;
  55.       weighted[c].avg /= num;
  56.     }
  57.     return {simple, weighted};
  58.   }
  59.  
  60. //  var states = ["IL"];
  61. //  var states = ["WI"];
  62. //  var states = ["MI", "WI"];
  63. //  var states = ['GA', 'NC', 'PA', 'NV', 'AZ'];
  64.   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'];
  65.   var jobs = [];
  66.   for (var i=0;i<states.length;i++) {
  67.     var url = 'https://politics-elex-results.data.api.cnn.io/results/view/2020-county-races-PG-'+states[i]+'.json';
  68.     jobs.push(fetch(url)
  69.               .then(res => res.json())
  70.               .then(data => {
  71.                 var state = data[0].stateAbbreviation;
  72.                 result_US[state] = count(data);
  73.                 console.log(result_US[state]);
  74.                 if (outer_result) outer_result.res = result_US;
  75.               }));
  76.   }
  77.   Promise.all(jobs).then(
  78.     _ => {
  79.       make_pseudo_cands(result_US.bens, result_US.votes);
  80.       result_US.props = make_props(result_US.bens, result_US.nof_counties);
  81.       result_US.diffs = make_diffs(result_US.props);
  82.       console.log(result_US);
  83.     }
  84.   );
  85. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement