# How to Fake Live Election Results with JavaScript

As today's California recall election demonstrates, live election results are a hugely popular feature for many news-oriented Web sites. This evening, I'll be reloading the California Secretary of State site over and over until my carpal tunnel nerves snap like kindling.

Offering live election results can be difficult if you don't have a source providing you this information, preferably in electronic form.

Fortunately, it's possible to use JavaScript to present authentic-looking fake live results that change over the course of Election Day.

Here's how:

First, decide how many votes will be cast in the election and counted on your page during the day. California is expecting around 10 million votes, 2 million of which have already been cast by absentee ballot but will not be counted during the day. So that leaves around 7-8 million votes. Store your total in a variable:

var y = 7850223;

Next, determine the time, in milliseconds since Jan. 1, 1970, at which the first vote should be reported. I accomplished this by publishing a dummy Web page with a three-line script:

var today = new Date();
var t = today.getTime();
document.write("<p>" + t);

This displays the time you need, in milliseconds. Store it as a variable named x. Here's mine, for around 7 a.m. PDT when the polls opened.

var x = 1065535367464;

Store the current time in a variable named t:

var today = new Date();
var t = today.getTime();

For the next step, you need to decide how many hours it will take to report 100 percent of the results. This value also needs to be in milliseconds, which are one-one thousandth of a second. For a 14-hour timeframe, it's 50400000 milliseconds (14 hours * 60 minutes * 60 seconds * 1000 milliseconds).

Using this value, the start time (x), and the current time (t), you can calculate the percentage of the vote that has been reported at the time the page is loaded:

var pct = (t - x) / 50400000;

To prevent the total from exceeding 100 percent after your time period, add this conditional:

if (pct > 1) {
pct = 1;
}

The percentage value will cycle from 0.0 to 1.0 over the course of your time period. Multiply it by 100 to get a percentage from 0 to 100 percent. It also can be used to determine the number of votes counted at any time, based on the total number that will be cast (y).

Store this in a variable named vc:

var vc = Math.floor(y * pct);

The Math.floor() function rounds down to a whole number. Without it, you'd be reporting partial votes, which only happens here in Florida.

Next, decide three things about each ballot item you are faking:

1. The percentage of the vote the item should receive when all votes have been counted.
2. The amount this value should change from the start of counting to the end.
3. Whether the item should be gaining ground during the day or losing it.

The last two things add a bit of variability to the results. If the percentage did not change during the day, your results wouldn't be believable. In a real election, that kind of thing could call the legitimacy of a perceived winner into question, which must not be allowed to happen in a democracy, regardless of the actual vote totals (see Bush v. Gore).

Here's a statement that causes a candidate to start with 12 percent of the vote at the beginning of counting and end up with 17 percent:

var jvp = 12 + (5 * pct);

Here's a statement that causes a candidate to start with 40 percent of the vote and end up with 33 percent:

var kvp = 40 - (7 * pct);

Once you have calculated the current percentage of the vote each item has received, you can use the number of counted votes (vc) to reverse-engineer a vote total for that item: var jv = Math.floor(vc * (jvp / 100));
var kv = Math.floor(vc * (kvp / 100));

The percentages you have calculated will have 5 or more decimal places, which is going to be intimidating to some people. We can't have that. The following JavaScript function will format a number to only two decimal places (like currency): function format(amount)
{
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
s = minus + s;
return s;
}
To finish, display the values you've calculated in document.write() statements, using the format() method with percentages: document.write("<table border="0" cellpadding="6"><tr valign="top">");
document.write("<tr><td>GARY COLEMAN</td><td>" + gv + "</td><td>" + format(gvp) + "%</td></tr>");
document.write("<tr><td>LARRY FLYNT</td><td>" + lv + "</td><td>" + format(lvp) + "%</td></tr>"); document.write("</tr></table>");

That's all there is to it. Your visitors will appreciate the live election results, which they can reload all day long to see who wins.

Su voto es su voz! Abrazos no drogas!

This tutorial is dedicated to Diebold Election Systems. If your no-paper-trail audit-proof touch-screen ballot tabulating system isn't a Diebold, it doesn't count.