Advertisement
Guest User

Untitled

a guest
Nov 16th, 2020
47
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.       make_pseudo_cands(result_US.bens, votes);
  16.       nof_counties++;
  17.       result_US.votes += votes;
  18.     });
  19.     result_US.nof_counties += nof_counties;
  20.     var props = make_props(bens, nof_counties);
  21.     return {bens, nof_counties, props, diffs:make_diffs(props)};
  22.   }
  23.   function make_pseudo_cands(bens, votes){
  24.     for (var i=0;i<cands.length;i++) countByCand(bens, {lastName:'C'+(cands[i]*100).toString().split('.')[0],
  25.                                                         voteNum: votes*cands[i],
  26.                                                         voteStr:(votes*cands[i]).toString()});
  27.   }
  28.   function countByCand(bens, cand){
  29.     if (cand.voteNum===0) return;
  30.     if (!bens[cand.lastName]) bens[cand.lastName] = {};
  31.     bens[cand.lastName][cand.voteStr[0]] = (bens[cand.lastName][cand.voteStr[0]]||0) +1;
  32.   }
  33.   var props_theoretical = {};
  34.   for (var i=1;i<10;i++) props_theoretical[i] = Math.log10(i+1)-Math.log10(i);
  35.   function make_props(bens, nof_counties){
  36.     var props = {};
  37.     for (var c in bens){
  38.       props[c] = {};
  39.       for (var i in bens[c]) props[c][i] = bens[c][i]/nof_counties;
  40.     }
  41.     return props;
  42.   }
  43.   function make_diffs(props){
  44.     var simple= {};
  45.     var weighted = {};
  46.     for (var c in props){
  47.       simple[c] = {avg:0};
  48.       weighted[c] = {avg:0};
  49.       var num = 0;
  50.       for (var i in props[c]) {
  51.         simple[c][i] = Math.abs(props[c][i]-props_theoretical[i]);
  52.         simple[c].avg += simple[c][i];
  53.         weighted[c][i] = simple[c][i]/props_theoretical[i];
  54.         weighted[c].avg += weighted[c][i];
  55.         num++;
  56.       }
  57.       simple[c].avg /= num;
  58.       weighted[c].avg /= num;
  59.     }
  60.     return {simple, weighted};
  61.   }
  62.  
  63. //  var states = ["IL"];
  64. //  var states = ["WI"];
  65. //  var states = ["MI", "WI"];
  66. //  var states = ['GA', 'NC', 'PA', 'NV', 'AZ'];
  67. //  var states = ['AZ','CO','FL','GA','IA','MI','MN','NV','NH','NC','OH','PA','TX','VA','WI'];
  68.   var states = ['VT', 'SC', 'IN', 'KY', 'GA', 'VA', 'WV', 'NC', 'OH', 'MA',
  69.                 'AL', 'FL', 'DE', 'MD', 'DC', 'NH', 'MS', 'RI', 'ME', 'NJ',
  70.                 'OK', 'PA', 'IL', 'CT', 'MO', 'TN', 'AR', 'NY', 'ND', 'CO',
  71.                 'MI', 'KS', 'NE', 'MN', 'TX', 'NM', 'WI', 'WY', 'LA', 'AZ',
  72.                 'SD', 'UT', 'MT', 'NV', 'IA', 'ID', 'OR', 'CA', 'WA', 'HI', 'AK'];
  73.   var jobs = [];
  74.   for (var i=0;i<states.length;i++) {
  75.     var url = 'https://politics-elex-results.data.api.cnn.io/results/view/2020-county-races-PG-'+states[i]+'.json';
  76.     jobs.push(fetch(url)
  77.               .then(res => res.json())
  78.               .then(data => {
  79.                 var state = data[0].stateAbbreviation;
  80.                 result_US[state] = count(data);
  81.                 console.log(result_US[state]);
  82.                 if (outer_result) outer_result.res = result_US;
  83.               }));
  84.   }
  85.   Promise.all(jobs).then(
  86.     _ => {
  87.       result_US.props = make_props(result_US.bens, result_US.nof_counties);
  88.       result_US.diffs = make_diffs(result_US.props);
  89.       console.log(result_US);
  90.     }
  91.   );
  92. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement