diff --git a/index.js b/index.js index ad3448c..078071c 100644 --- a/index.js +++ b/index.js @@ -17,13 +17,13 @@ const f = response.url.split('/').pop(); if (f === "metadata.json") { const metadata = await response.json(); - states = new Map(metadata.geo.map((c) => [c.uid, c.name])) + states = new Map(metadata.geo.map((c) => [c.uid, c.name])); parties = new Map(metadata.parties.map((c) => [c.code, c.name])); candidates = new Map(metadata.candidates.map((c) => [c.uid, [c.fullName, parties.get(c.candidateRaces[0].majorParty)]])); } else if (f === "president.json") { const president = await response.json(); - let data = president[0]["state_electionTypes"].map((t) => t.state.officeRaces[0].candidateVotes.map((o) => { + let data = president[0].state_electionTypes.map((t) => t.state.officeRaces[0].candidateVotes.map((o) => { return { "name": (candidates) ? candidates.get(o.candidate) : [o.candidate, "?"], "votes": o.totalVote @@ -40,7 +40,7 @@ return acc; }, []); - let stateVotes = president[0]["state_electionTypes"].map((t) => { + let stateVotes = president[0].state_electionTypes.map((t) => { return { "state": (states) ? states.get(t.state.uid) : t.uid, "current": t.state.officeRaces[0].officeVotes[0].totalVote, @@ -77,9 +77,15 @@ stateContainer.innerHTML = ''; stateVotes.forEach((row) => { const table = document.createElement('table'); + const caption = document.createElement('caption'); + caption.innerHTML = ` +

${row.state} (Votes Left: ${row.expected - row.current})

+ `; + table.appendChild(caption); + const thead = document.createElement('thead'); const headerRow = document.createElement('tr'); - const headers = [`${row.state}`, 'Party', `Votes Left: ${row.expected - row.current}`]; + const headers = ['Name', 'Party', 'Votes']; headers.forEach(header => { const th = document.createElement('th'); th.textContent = header; @@ -89,7 +95,7 @@ table.appendChild(thead); const tbody = document.createElement('tbody'); - row.candidates.sort((a, b) => b.votes - a.votes).forEach((candidate, index) => { + row.candidates.sort((a, b) => b.votes - a.votes).forEach((candidate) => { const rowElement = document.createElement('tr'); const nameCell = document.createElement('td');