平均税计算器

时间:2013-08-16 16:56:52

标签: javascript html xhtml

所以我正在尝试制作税收计算器,以计算平均税和边际税。但我遇到了一些问题。

首先是我的代码:

<style>
  span{font-style:italic;}
  span{color:green;}
  table{border: 1px solid black;}
</style>

<script>
//decimal code
var calculated = false;
window.onload = function () {
    document.getElementById('dec').addEventListener('change', function () {
        if (calculated) calcul();});   
} //end of decimal code
function calcul() {
        //Taxable Income
        var TaxInc = parseFloat(document.getElementById("TaxInc").value);
        //Inkomen box 1
        var Income1 = parseFloat(document.getElementById("Income1").value);
        //Tax Box 1
        var box1 = parseFloat(document.getElementById("box1").value);
        //Linkergrens inkomen box 2
        var Income2 = parseFloat(document.getElementById("Income2").value);
        //Rechtergrens inkomen box 2
        var Income21 = parseFloat(document.getElementById("Income21").value);
        //Tax Box 2
        var box2 = parseFloat(document.getElementById("box2").value);
        //Inkomen box 3
        var Income3 = parseFloat(document.getElementById("Income3").value);
        //Tax Box 3
        var box3 = parseFloat(document.getElementById("box3").value);
        //Income 1 taxed
        var Inc1Taxed = TaxInc * box1;
        //Tax
        var Tax;
        if (TaxInc >= 0 && TaxInc <= Income1) {
            Tax = (Inc1Taxed)/100;
        }
        else if (TaxInc >= Income2 && TaxInc <= Income21) {
            Tax = (Inc1Taxed/100)+(((TaxInc - Income1)* box2)/100);
        }
        else (TaxInc >= Income3) {
            Tax = ((Inc1Taxed/100) + 
            (((TaxInc - Income1)* box2)/100) + 
            (((TaxInc + Income21 - Income2 - Income1)* box3)/100));
        }
        //decimal code
        var decs = +(document.getElementById('dec').value);
        calculated = true; //end of decimal code

        document.getElementById("answer").innerHTML = Tax.toFixed(decs);

       }
</script>

<form action="" id="nothing">
The Dutch Firm Herrera NV had
<input type="text" id="TaxInc" maxlength="10" size="10">
in taxable income. Using Table 3.3, what is the company's average tax rate? 
What is its marginal tax rate?<br />
<br />
<table>
    <tr>
        <td>
        <strong>Taxable income</strong>
        </td>
        <td>
        <strong>Tax percentage</strong>
        </td>
    </tr>

    <tr>
        <td>
        0 - 
        <input type="text" id="Income1" maxlength="5" size="5">
        </td>
        <td>
        <input type="text" id="box1" maxlength="5" size="5">
        </td>
    </tr>

    <tr>
        <td>
        <input type="text" id="Income2" maxlength="5" size="5"> - 
        <input type="text" id="Income21" maxlength="5" size="5">
        </td>
        <td>
        <input type="text" id="box2" maxlength="5" size="5">        
        </td>
    </tr>

    <tr>
        <td>
        <input type="text" id="Income3" maxlength="5" size="5">
          =>
        </td>
        <td>
        <input type="text" id="box3" maxlength="5" size="5">
        </td>
    </tr>
</table>
<br />
<br />
    <input type="button" value="Calculate" id="but" onclick="calcul()" />
<br />
<br />
</form>
<br />
<br />
The answer is <span id="answer">XXX</span>
<br />
<br />
    <br />
    Select the amount of decimals <select id="dec">
      <option value="0">0 decimals</option>
      <option value="1">1 decimal</option>
      <option value="2">2 decimals</option>
      <option value="3">3 decimals</option>
      <option value="4">4 decimals</option>
      <option value="5">5 decimals</option>
      <option value="6">6 decimals</option>
    </select>
    <br />

我的意图是什么?首先,用户应按以下规则输入应税收入:

The Dutch Firm Herrera NV had
<input type="text" id="TaxInc" maxlength="10" size="10">
in taxable income.
I've tried to run in it jsFiddle, which is a very handy website.

然后他们需要在表格中给出所得税“盒子”的范围。所以第一个可以是0 - 10000,第二个可以是10000 - 25000,第三个可以是25000 - >。在它旁边的栏中,他们需要给出税率/百分比。

由于我想尽可能动态地尝试,因此税收的公式也应该是“动态的”。这就是我使用If...., else if ...., else语句来指定范围的原因。如果收入超过If语句中的金额,它将查看else if语句。

(不是那么)有趣的是,当我只插入前两个语句的公式(If和else If)时,它可以完美地运行。但是一旦出现第三个公式,计算按钮就会被打破。我一遍又一遍地看着这个公式,但在我看来,似乎没有错误。

由于我在Google协作平台中使用此代码,因此内置编辑器会向我返回错误,并在以下规则中添加分号: else (TaxInc >= Income3) {

但是,如果我这样做,它将关闭其余的代码,代码肯定不会工作。我认为第三个'其他'陈述中有一个错误,但是我找不到。

对于边际税率:边际税率是应税收入增加一欧元时所需的税率。一个简单的想法是只需通过TaxInc添加+1,但是这样,我必须复制If语句并将所有TaxInc更改为TaxInc +。是否有“更短/更简单”的方式呢? (我只有基本的javascript和html知识)。

0 个答案:

没有答案