|
// building a keystrokes object for each period of time |
|
function getKeystrokes(keys, hours, hours_per_day, days_per_week) { |
|
var |
|
ratio = keys / hours, |
|
hours_per_week = hours_per_day * days_per_week; |
|
return { |
|
"hourly" : ratio, |
|
"daily" : ratio * hours_per_day, |
|
"weekly" : ratio * hours_per_week, |
|
"annually": ratio * hours_per_week * 52.1775 |
|
} |
|
} |
|
|
|
// building a distance object for each measure |
|
function getDistances(keys, keystroke_distance) { |
|
var inches = keys * keystroke_distance; |
|
return { |
|
"inches": inches, |
|
"feet" : inches / 12, |
|
"miles" : inches / 63360 |
|
} |
|
} |
|
|
|
// getting |
|
function getResults() { |
|
var |
|
// get input values |
|
$hours = document.getElementById("hours").value, |
|
$keystrokes = document.getElementById("keystrokes").value, |
|
$hours_coding = document.getElementById("hours_coding").value, |
|
$days_coding = document.getElementById("days_coding").value, |
|
$keystroke_distance = document.getElementById("keystroke_distance").value, |
|
// do maths |
|
keystrokes = getKeystrokes($keystrokes, $hours, $hours_coding, $days_coding), |
|
h_distances = getDistances(keystrokes.hourly, $keystroke_distance), |
|
d_distances = getDistances(keystrokes.daily, $keystroke_distance), |
|
w_distances = getDistances(keystrokes.weekly, $keystroke_distance), |
|
a_distances = getDistances(keystrokes.annually, $keystroke_distance) |
|
// results object |
|
results = { |
|
"keystrokes": keystrokes, |
|
"hourly" : h_distances, |
|
"daily" : d_distances, |
|
"weekly" : w_distances, |
|
"annually" : a_distances |
|
}; |
|
// return results object |
|
return results; |
|
} |
|
|
|
// output results to dom |
|
function outputResults() { |
|
var |
|
results = getResults(), |
|
key = /(\"[a-z]*\")/g, |
|
decimal = /(\d{1,}\.?\d{1,})/g, |
|
// syntax highlighting |
|
formatted_results = JSON.stringify(results, null, " ") |
|
.replace(key, "<em class=\"key\">$1</em>") |
|
.replace(decimal, "<em class=\"val\">$1</em>"); |
|
// write shit to the DOM |
|
document.getElementById("output").innerHTML = formatted_results; |
|
} |
|
|
|
// for each input |
|
var inputs = document.getElementsByTagName("input"); |
|
for (var i = 0; i < inputs.length; i++) { |
|
// whenever the input changes |
|
inputs[i].oninput = function() { |
|
// form complete to be invalidated on empty input |
|
var form_complete = true; |
|
// check if we have all the data |
|
for (var ii = 0; ii < inputs.length; ii++) { |
|
if (!inputs[ii].value) { form_complete = false; } |
|
} |
|
// if we have all the data |
|
if (form_complete) { outputResults(); } |
|
} |
|
} |
|
|
|
// initial results |
|
outputResults(); |
|
|
|
|
|
jakealbaughSignature(); |