Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 让我的计算器工作_Javascript_Calculator - Fatal编程技术网

Javascript 让我的计算器工作

Javascript 让我的计算器工作,javascript,calculator,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/xht

好的,我这里有一段代码,我要做的是读入一个字符,表示超大尺寸,如果值是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>

就风格而言,而不是:

                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%,但其他所有操作都是相同的。其次,在这里添加日志或输出语句以调试环境中的真正问题要容易得多


干杯。

我没有看到任何地方声明ssize?ssize是一个输入元素;它永远不能等于字符串y;但是每次我运行代码时,它是否会继续将价格乘以1.5?既然是这样,那就去吧。看看会发生什么。这听起来像是关于传递值和传递引用语义的问题。请看一些其他答案,以明确主题: