Published
Edited
Aug 27, 2020
Also listed in…
On Politics
Insert cell
md`# How the JJB might have won 10 seats`
Insert cell
{
let mdStr = md``;

await Promise.all(
ED_ID_LIST.map(async function(regionID) {
const partyToVotes = await getPartyToVotes(YEAR, regionID);
const totalVotes = sum(Object.values(partyToVotes));
const actualVotes = partyToVotes[PARTY] ? partyToVotes[PARTY] : 0;
let actualPartyToSeats = getPartyToSeats(YEAR, regionID, partyToVotes);
if (!actualPartyToSeats[PARTY]) {
actualPartyToSeats[PARTY] = 0;
}
const actualSeats = actualPartyToSeats[PARTY];

mdStr = md`${mdStr}
### ${getRegionLabel(regionID)}
*${actualSeats} seats, ${formatNumber(actualVotes)} votes, ${formatPercent(
actualVotes / totalVotes
)}*
`;

const INCR_VOTES = 100;
const MAX_EXTRA_SEATS = 3;
let prevHypoSeats = actualSeats;
let prevHypoVotes = actualVotes;
let hypoPartyToVotes = partyToVotes;

let i = 0;
while (true) {
i += 1;
if (i > 1000) {
break;
}
hypoPartyToVotes[PARTY] += INCR_VOTES;

const hypoPartyToSeats = getPartyToSeats(YEAR, regionID, partyToVotes);
const hypoSeats = hypoPartyToSeats[PARTY];
const hypoVotes = hypoPartyToVotes[PARTY];

const pExtra = (hypoVotes - actualVotes) / actualVotes;
if (pExtra > 0.5) {
break;
}

if (prevHypoSeats < hypoSeats) {
mdStr = md`${mdStr}
${hypoSeats} seats - another **${formatNumber(
hypoVotes - prevHypoVotes
)} votes (+${formatPercent(pExtra)})**, for a total of ${formatNumber(
hypoVotes
)} votes, ${formatPercent(
hypoVotes / (totalVotes - actualVotes + hypoVotes)
)}`;

prevHypoSeats = hypoSeats;
prevHypoVotes = hypoVotes;
}

if (hypoSeats - actualSeats >= MAX_EXTRA_SEATS) {
break;
}
}
})
);
return mdStr;
}
Insert cell
import { range, sum } from '@nuuuwan/list-utils'
Insert cell
import { formatPercent, formatNumber } from '@nuuuwan/string-utils'
Insert cell
import { getRegionLabel } from '@nuuuwan/sri-lanka-regions'
Insert cell
import {
ED_ID_LIST,
getPartyToVotes,
getPartyToSeats
} from '@nuuuwan/sl-elections'
Insert cell
YEAR = 2020
Insert cell
PARTY = 'JJB'
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more