var numDies = 0; // master counter for our number of dies // This function gets the reduction of area with two provided sizes, and returns it function getReduction(startSize, finalSize) { var startArea = Math.PI * ((startSize / 2) * (startSize / 2)); var finalArea = Math.PI * ((finalSize / 2) * (finalSize / 2)); return ((startArea - finalArea) / startArea) * 100; } // This function gets the elongation function getElongation(startSize, finalSize) { return (Math.pow(startSize / finalSize, 2) - 1) * 100; } function addReduction() { numDies ++; // format our HTML var html = ""; // start with an empty html html += ""; html += ""; // create the row var table = document.getElementById("data"); var row = table.insertRow(-1); var cell1 = row.insertCell(0); cell1.innerHTML = html; } function removeReduction() { // function to remove the last row numDies --; document.getElementById("data").deleteRow(-1); // delete the last row in the table } function doMath() { outputTable = document.getElementById("output"); // Select our output data table outputTable.innerHTML = ""; var row = []; row[0] = outputTable.insertRow(0); row[0].id = "outputHeader"; // set the header row var r1c1 = row[0].insertCell(0); //blank var r1c2 = row[0].insertCell(1); // "Start -> Finish" var r1c3 = row[0].insertCell(2); // "ROA" var r1c4 = row[0].insertCell(3); // "Elong" var r1c5 = row[0].insertCell(4); // "Delta" // Create the header of the table r1c1.innerHTML = ""; r1c1.id = "headerSpacer"; r1c2.innerHTML = "Start -> Finish"; r1c2.id = "startFinish"; r1c3.innerHTML = "ROA (%)"; r1c3.id = "roa"; r1c4.innerHTML = "Elong (%)"; r1c4.id = "elong"; r1c5.innerHTML = "Delta**"; r1c5.id = "delta"; for (var i = 1; i < numDies + 1; i++) { inSize = document.getElementById("die" + (i - 1)).value; // the input size outSize = document.getElementById("die" + i).value; // output size if (i == 1 && document.getElementById("metric").checked == true) { // If this is the first die in the setup, check if it's a metric/rod start and convert it inSize = toInches(inSize); } // Format our numbers to prevent maximum user stupidity if (inSize < 10.0 && inSize > 0) { // If we have a 'proper' number i.e. ".130" inSize = Number(inSize); } else { // re-format the number if it's 'wrong' i.e. "130" inSize = inSize / 1000; } if (outSize < 10.0 && outSize > 0) { // If we have a 'proper' number i.e. ".130" outSize = Number(outSize); } else { // re-format the number if it's 'wrong' i.e. "130" outSize = outSize / 1000; } console.log(inSize + " " + outSize); row[i] = outputTable.insertRow(i); var cell1 = row[i].insertCell(0); var cell2 = row[i].insertCell(1); cell2.id = "startFinish"; var cell3 = row[i].insertCell(2); cell3.id = "roa"; var cell4 = row[i].insertCell(3); cell4.id = "elong"; cell1.innerHTML = "Reduction " + i + ":"; cell2.innerHTML = inSize + "\" (" + toMillimetres(inSize) + " mm) -> " + outSize + "\" (" + toMillimetres(outSize) + " mm)"; cell3.innerHTML = Math.round(getReduction(inSize, outSize) * 100) / 100; cell4.innerHTML = Math.round(getElongation(inSize, outSize) * 100) / 100; } } function toMillimetres(size) { //convert to mm size = Math.round((size * 100) * 25.4) / 100; return size; } function toInches(size) { //convert to inches size = Math.round((size * 1000) / 25.4) / 1000; return size; }