Update: revised code.
I work at a Starbucks and one of the things that screams at me week after week for automation is the tedious calculation of the tip distribution. So here is my attempt to provide a simple tool for this. (Previous attempt required special software and special instruction to explain how it works.)
It has fields for inputting the number of hours for each partner and the amount of money to be dispersed. Clicking "calculate" produces the output with blank lines to help orient the output with the input rows.
As a javascript noob, I feel certain I've done some stupids in here I can learn from.
<html>
<head>
<title>Tip Calculator</title>
</head>
<body onload="loaded()">
<h3>Tip Calculator</h3>
Hours:<br>
<table id=inputs>
</table>
<table id=controls>
<tr>
<td><input type=button value="add row" onclick="add_row()">
<td>Dividend: <input id=dividend type=text size=5 maxlength=5>
<td><input type=button value="calculate" onclick="calc()">
</tr>
</table>
<span id=output colspan=3> </span>
</body>
<script language="JavaScript">
<!--
var num_inputs = 6;
function loaded() {
add_row();
}
function add_row(){
var table = E('inputs');
var new_row = table.insertRow( table.rows.length );
for( var i=0; i<6; i++) {
new_row.insertCell(i).innerHTML = "<input type=text size=5 maxlength=5>";
}
}
function calc(){
var sum = 0;
var inputs = E('inputs').getElementsByTagName('input');
for( var i=0; i < inputs.length; i++ ){
sum += Number(inputs[i].value);
}
rate = E('dividend').value / sum;
E('output').innerHTML = "Total Hours: " + sum + "<br>";
E('output').innerHTML += "Rate (Dividend/Total Hours): " + rate + "<br>";
for( var i=0; i < inputs.length; i++ ){
if( (i % 6) == 0 ) E('output').innerHTML += "<br>";
share = Number( inputs[i].value ) * rate;
E('output').innerHTML += inputs[i].value + " × " + rate + " = " + share + "<br>";
}
}
function E( id ){
return document.getElementById( id );
}
//-->
</script>
</html>