そこで、平均税と限界税を計算する税計算機を作ろうとしています。しかし、私はいくつかの問題に遭遇しています。
最初に私のコード:
<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、2 番目は 10000 から 25000、3 番目は 25000 -> になります。その横の列に、税率/パーセンテージを記入する必要があります。
できるだけ動的に試したいので、税率の式も「動的」にする必要があります。そのためIf...., else if ...., else
、範囲を指定するためにステートメントを使用しました。収入が If ステートメントの金額を超える場合は、else if ステートメントを参照します。
(そうではない) 面白い点は、最初の 2 つのステートメント (If と else If) の数式のみを挿入すると、問題なく動作することです。しかし、3 番目の数式が表示されるとすぐに、計算ボタンが壊れます。公式は何度も見直しましたが、間違いはないように思います。
このコードを Google サイトで使用しているため、組み込みのエディタから次のルールにセミコロンを挿入するようにというエラーが返されました。
else (TaxInc >= Income3) {
しかし、それを行うと、残りのコードが切り取られ、コードは確実に機能しなくなります。3番目の「else」ステートメントにエラーがあると思いますが、何がわかりません。
限界税率について: 限界税率は、課税所得が 1 ユーロ上昇するときに必要な税率です。簡単な考えでは、TaxInc で +1 を追加するだけですが、その方法では、If ステートメントをコピーして、たとえばすべての TaxInc を TaxInc+ に変更する必要があります。「より短い/より簡単な」方法もありますか? (私は基本的なjavascriptとhtmlの知識しか持っていません)。