Performing calculation within a loop using previous loop values


I am not an expert here and need another set of eyes to see what I am doing wrong.
Overview of the objective is to generate a multi-tier pricing structure using a known high price, low price and number of steps.

So here goes, and hopefully someone can follow how I am attempting accomplish this.
Here is a stripped down version of my JS (including only the variables/parts relevant to this operation)

<script type="text/javascript">
/*purpose is to calculate multi-tier price struture based on several factors*/
function cal() { 
oFormObject = document.forms['pricing']; /*get the input form*/
var vtiers = parseInt(oFormObject.elements["tiers"].value); /*set var for number of steps to use*/
var vtier = vtiers; /*set second step var to use for price calculation*/
vtiers=vtiers+1; /*vtiers is used for dynamic table creation, so we are adding 1 to accomidate table header row*/
var vcase_count = parseInt(oFormObject.elements["case_count"].value); /*How many peices per case or tray. to be used in setting volume breaks*/
var rows_count = parseInt(document.getElementById("price_data").getElementsByTagName("tr").length);
if(rows_count < vtiers){
var row0 = rows_count;
var cell0 = 0;
for(i = rows_count; i < vtiers; i++){
var table1 = document.getElementById("price_data");
    var row1 = table1.insertRow(row0);
    var cell1 = row1.insertCell(cell0);
    cell0 = cell0+1;
    var cell2 = row1.insertCell(cell0);
    cell0 = cell0+1;
    var cell3 = row1.insertCell(cell0);
    cell0 = cell0+1;
    var cell4 = row1.insertCell(cell0);
    cell0 = 0;"right";"center";"center";"left";
    cell1.className = "newformlabel";
    cell2.className = "newformlabel";
    cell3.className = "newformlabel";
    cell4.className = "newformlabel";
    cell1.innerHTML = "<input type='text' name='Min_Qantity"+row0+"' id='Min_Qantity"+row0+"' value=''>";
    cell2.innerHTML = "<input type='text' name='Max_Qantity"+row0+"' id='Max_Qantity"+row0+"' value=''>";
    cell3.innerHTML = "<input type='text' name='price"+row0+"' id='price"+row0+"' value=''>";
    cell4.innerHTML = "<input type='text' name='discount"+row0+"' id='discount"+row0+"' value=''>";
    row0 = row0+1;
if(rows_count > vtiers+1){
    while(rows_count > vtiers){
    document.getElementById("price_data").deleteRow(rows_count -1);
    rows_count = rows_count-1;
/*Once selling prices have been calculated in the form, will need to manually enter actual prices to be used so the can be rounded or modified as seen fit*/
var set_low = parseFloat(oFormObject.elements["low_price2"].value).toFixed(2); /*get the manually entered low price into a variable*/
var set_high = parseFloat(oFormObject.elements["high_price2"].value).toFixed(2); /*get the manually entered high price into a variable*/
var spread_percent = parseFloat(100-(100/(set_high/set_low))).toFixed(3); /*calculate the percentage discount between high & low price*/
var set_point = parseFloat((spread_percent/vtier)/100).toFixed(3); /*divide the spread_percent value by the number of breaks/tiers finishing in a decimal value*/
    var rows_count = parseInt(document.getElementById("price_data").getElementsByTagName("tr").length);
    oFormObject1 = document.forms['pricing'];
    var vtier = parseInt(oFormObject1.elements["tiers"].value);
    var vtiers = vtier+1;
    var row0 = 1;
    var row0a = 0;
    if(row0 = 1){
    oFormObject1.elements["Min_Qantity"+row0].value = "2";
    oFormObject1.elements["Max_Qantity"+row0].value = vcase_count-1;
    oFormObject1.elements["price"+row0].value = set_high;
    oFormObject1.elements["discount"+row0].value = set_point;
        for(i = 1; i < vtiers; i++)        {
            var p = parseInt(oFormObject1.elements["Max_Qantity"+row0a].value);
            var d = parseFloat(oFormObject1.elements["discount"+row0a].value).toFixed(3);
            oFormObject1.elements["Min_Qantity"+row0].value = p+1;
            oFormObject1.elements["Max_Qantity"+row0].value = p+vcase_count;
            oFormObject1.elements["discount"+row0].value = parseFloat(d+set_point).toFixed(3);
            var dis = parseFloat(d+set_point).toFixed(3);
            oFormObject1.elements["price"+row0].value = parseFloat(set_high-(set_high*dis)).toFixed(2);


Hopefully someone can follow.
The “Min_Qantity” & “Max_Quantity” fields are setting themselves as expected in the for(i = 1; i < vtiers; i++) loop, but the discount value is not.

The matching form that works with this is as follows:

<form id="pricing" name="pricing" method="post" action=""><table border="0" align="center" cellpadding="5">
    <td align="right">How many discount steps?:    </td>
    <td><input name="tiers" id="tiers" type="text" value="6" size="3" /></td>
    <td align="right">Number Pieces Per Case:</td>
    <td><input name="case_count" type="text" id="case_count" onmouseover="cal()" value="36" size="7" /></td>
  <tr align="right">
    <td colspan="4"><hr /></td>
    <td align="right">Set High Price:    </td>
    <td><input name="high_price2" type="text" id="high_price2" onmouseover="cal()" value="19.95" size="8" /></td>
    <td align="right">Set Low Price: </td>
    <td><input name="low_price2" type="text" id="low_price2" onmouseover="cal()" value="4.45" size="8"  /></td>
    <table id="price_data" width="100%" border="1" cellspacing="0" cellpadding="0">
    <th scope="col">Minimun</th>
    <th scope="col">Maximum</th>
    <th scope="col">Price</th>
    <th scope="col">Discount</th>
</table><div id="structure"></div>


Thank you in advance

Just from a quick scan I see you are using toFixed(3). toFixed returns a string, not sure if that is the cause of your problem.

You could try wrapping it in brackets and using a ‘+’ sign to coerce the returned string to a number


As I say I’ve only had a cursory look.

Thank you. Shortly after posting this I finally stumbled onto a direction toward the answer and it’s all functioning now. It was my not understanding when/how to use what (parseInt, parseFloat, Number)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.