Tip Calculator in javascript 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.



&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Tip Calculator&lt;/title&gt;
  &lt;/head&gt;

  &lt;body onload="loaded()"&gt;
    &lt;h3&gt;Tip Calculator&lt;/h3&gt;
    Hours:&lt;br&gt;
    &lt;table id=inputs&gt;
    &lt;/table&gt;

    &lt;table id=controls&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;input type=button value="add row" onclick="add_row()"&gt;
        &lt;td&gt;Dividend: &lt;input id=dividend type=text size=5 maxlength=5&gt;
        &lt;td&gt;&lt;input type=button value="calculate" onclick="calc()"&gt;
      &lt;/tr&gt;
    &lt;/table&gt;

    &lt;span id=output colspan=3&gt;&amp;nbsp;&lt;/span&gt;

  &lt;/body&gt;
  &lt;script language="JavaScript"&gt;
  &lt;!--

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&lt;6;  i++) {
    new_row.insertCell(i).innerHTML = "&lt;input type=text size=5 maxlength=5&gt;";
  }
}

function calc(){
  var sum = 0;
  var inputs = E('inputs').getElementsByTagName('input');

  for(  var i=0;  i &lt; inputs.length;  i++  ){
    sum += Number(inputs[i].value);
  }
  rate = E('dividend').value / sum;
  E('output').innerHTML = "Total Hours: " + sum + "&lt;br&gt;";
  E('output').innerHTML += "Rate (Dividend/Total Hours): " + rate + "&lt;br&gt;";
  for(  var i=0;  i &lt; inputs.length;  i++  ){
    if(  (i % 6) == 0  )  E('output').innerHTML += "&lt;br&gt;";
    share = Number( inputs[i].value ) * rate;
    E('output').innerHTML += inputs[i].value + " &amp;times; " + rate + " = " + share + "&lt;br&gt;";
  }
}

function E( id ){
  return document.getElementById( id );
}

  //--&gt;
  &lt;/script&gt;

&lt;/html&gt;
What you could do first is to separate out the logic that does the calculation from the logic that handles the UI. This makes it easier to update the math. Also, this makes the logic portable, should you scrap the UI code for something else, like if you move to use a framework.

Also, break up the logic to small functions that accept a predictable input. For instance, calculating shares is simply multiplying your inputs by your dividend - an array of input values and a number.

// In goes an array of hours and the dividend, out comes an array of shares
function getShares(hours, dividend){
  const sum = hours.reduce((c, v) =&gt; c + v);
  const rate = dividend / sum;
  const shares = hours.map(hour =&gt; hour * rate);
  return shares;
}


I also notice this strange E function which is just short for document.getElementById. There is document.querySelector and document.querySelectorAll that allows you to use CSS selectors to get a reference of your elements. Also, I'd avoid IDs since IDs should only appear once on the page. You can't use it for multiple elements of the same kind, for instance your inputs. Use classes instead.

For this case, I would tolerate the innerHTML usage. Otherwise, construct elements using document.createElement. Now to create repeated HTML, you can use array.map and array.join with template literals.

function renderResults(results){
  return results.map(result =&gt; `
    &lt;div class="result"&gt;${result.hours} x ${result.dividend} = ${result.share}&lt;/div&gt;
  `).join('');
}


For your HTML, while there are cases where the quotes on attributes are optional, it's best you put them in for consistency.