让我的计算器工作

时间:2014-11-10 21:31:31

标签: javascript calculator

好的,所以我在这里有这个代码,我想要做的是在字符中读取超大尺寸,如果值为" y"然后将价格乘以1.5乘以显示百分之五十的增长。我不确定我的错误在哪里....

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org     /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script>

var products=[0,2,3,5,7,8,9,12];
var prices=[3.59,4.99,2.59,1.99,2.99,4.29,3.19,5.29];
var taxes=.06;

var forimages=new Array();

for (var j=0;j<products.length;j++) {
    forimages[j]=new Image(200,200);
    forimages[j].src="chesssmall.jpg";
    }


var thequantity=0;
var thebill=0;
var thetax=0;
var thetotal=0;

var thecode;
var hits=0;

var qbox;

function computebill(form) {

    qbox=document.getElementById("quantity");
    hits=0; 
    thecode=parseFloat(form.code.value);    
    thequantity=parseFloat(form.numbuy.value);


for (var i=0;i<=products.length;i++) {

    //why is there an increment thing on the indidual variable below

    if (thecode==products[i]){
    document.main.src=forimages[i].src;

    if(ssize=="y"){

    form.price.value=prices[i]; 


    thebill+=(prices[i]*1.5)*thequantity;
    form.bill.value=thebill.toFixed(2);


    thetax+=(prices[i]*1.5)*thequantity*taxes[i];
    form.tax.value=thetax.toFixed(2);

    thetotal=thebill+thetax;
    form.total.value=thetotal.toFixed(2);
    }
    else{


    form.price.value=prices[i]; 


    thebill+=(prices[i])*thequantity;
    form.bill.value=thebill.toFixed(2);


    thetax+=(prices[i])*thequantity*taxes[i];
    form.tax.value=thetax.toFixed(2);

    thetotal=thebill+thetax;
    form.total.value=thetotal.toFixed(2);
    }

    hits=1; 
    break;
    }
}

if (hits==0) {
    form.code.value="Item Not Found";

        }

    form.code.focus();
    form.code.select();
}

</script>

<style type="text/css">
.ql {
    background-color: #fff;
}
</style>


</head>

<body>
<div style="float:left">
 <form>
  <table width="377" border="0">
    <tr>
      <td width="158">Enter Product Code</td>
      <td width="232"><input type="text" name="code" id="code" /></td>
    </tr>
    <tr>
      <td>Quantity Buying</td>
      <td><input type="text" name="numbuy" id="numbuy" /></td>
    </tr>    
    <tr>
      <td>Supersize?</td>
      <td><input type="text" name="ssize" id="ssize" /></td>
    </tr>
    <tr>
      <td>The Price</td>
      <td><input type="text" name="price" id="price" /></td>
    </tr>
    <tr>
      <td>Bill</td>
      <td><input type="text" name="bill" id="bill" /></td>
    </tr>
    <tr>
      <td>Tax      </td>
      <td><input type="text" name="tax" id="tax" /></td>
    </tr>
    <tr>
      <td>Total Bill</td>
      <td><input type="text" name="total" id="total" /></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><input type="button" name="forsum" id="forsum" value="Add" onclick="computebill(this.form)"/></td>
    </tr>
  </table>

</form>
</div>
<img src="window2.jpg" width="200" height="200" name="main" id="main"/>
</body>
</html>

1 个答案:

答案 0 :(得分:-1)

在风格上,而不是:

                if(ssize=="y"){

                        form.price.value=prices[i];


                        thebill+=(prices[i]*1.5)*thequantity;
                        form.bill.value=thebill.toFixed(2);


                        thetax+=(prices[i]*1.5)*thequantity*taxes[i];
                        form.tax.value=thetax.toFixed(2);

                        thetotal=thebill+thetax;
                        form.total.value=thetotal.toFixed(2);
                }
                else{


                        form.price.value=prices[i];


                        thebill+=(prices[i])*thequantity;
                        form.bill.value=thebill.toFixed(2);


                        thetax+=(prices[i])*thequantity*taxes[i];
                        form.tax.value=thetax.toFixed(2);

                        thetotal=thebill+thetax;
                        form.total.value=thetotal.toFixed(2);
                }

这样做:

                var theprice = prices[i];
                if(ssize=="y"){
                        theprice *= 1.5;
                }


                form.price.value=theprice;

                thebill+=theprice*thequantity;
                form.bill.value=thebill.toFixed(2);


                thetax+=theprice*thequantity*taxes[i];
                form.tax.value=thetax.toFixed(2);

                thetotal=thebill+thetax;
                form.total.value=thetotal.toFixed(2);

有两个理由表明这一点。首先,您的代码说明了它意味着什么 - 即,在一个特殊情况下,增加50%的价格,但其他一切都相同。其次,在此处添加日志记录或输出语句以调试环境真正的错误要容易得多。

干杯。