【问题标题】:getting my calculator to work让我的计算器工作
【发布时间】:2014-11-10 21:31:30
【问题描述】:

好的,所以我在这里有这段代码,我想要做的是读取一个字符以获得超大尺寸,如果值为“y”,则将价格乘以 1.5 以显示 50% 的增长。我不确定我的错误在哪里......

   <!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>

【问题讨论】:

  • 我在任何地方都没有看到 ssize 声明?
  • ssize 是输入元素;它永远不能等于 "y" 这是一个字符串。
  • 你应该像这样在你的 javascript 中声明 ssize:var ssize = document.getElementById("ssize").value;

标签: javascript calculator


【解决方案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%,但其他一切都相同。其次,在此处添加日志记录或输出语句来调试您的环境的真正问题要容易得多。

干杯。

【讨论】:

  • 但是每次我运行代码,它不会继续将价格乘以 1.5 吗?因为它循环
  • 试试看。走着瞧吧。这听起来像是一个关于按值传递与按引用传递语义的问题。看看其他一些答案以了解该主题:stackoverflow.com/questions/6605640/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-12
  • 2016-05-17
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多