do {
entry = prompt("Enter taxable income as a valid number\n" +
"Or enter 99999 to end entries", 99999);
entry = parseInt(entry);
// calculate the tax owed here
if (entry < 0){
document.write("Please enter a positive number");
}
if (entry > 0 && entry < 8701){
tax_owed = entry * 0.10;
}
if (entry > 8700 && entry < 35351){
tax_owed = 870 * 0.15;
tax_owed += entry;
}
if (entry > 35350 && entry < 85651){
tax_owed = 4867 * 0.25;
tax_owed += entry;
}
if (entry > 85650 && < 178651){
tax_owed = 17442 * 0.28;
tax_owed += entry;
}
if (entry > 178650 && entry < 388351){
tax_owed = 43482 * 0.33;
tax_owed += entry;
}
if (entry > 388350){
tax_owed = 112683 * 0.35;
tax_owed += entry;
}
alert("Tax owed is " + tax_owed);
}
while (entry != 99999);
答案 0 :(得分:1)
在你的21行
if (entry > 85650 && < 178651){ // there is no operand between && and <.
我认为应该是
if (entry > 85650 && entry< 178651){
答案 1 :(得分:1)
在if (entry > 85650 && < 178651)
行上如果仔细观察,会发现您没有将第二个数字与任何数字进行比较。它应该是if (entry > 85650 && entry < 178651)
答案 2 :(得分:1)
已找到< 178651
之前缺少操作数的错误,但如果您希望代码具有较少的潜在错误,我建议进行轻量级重构。
使用else if
会更有效率。一旦找到对应于给定entry
值的块,程序将停止检查其他条件。此外,您只需指定区分范围的值(您的&#34;里程碑&#34;),而不是像entry < 8701
后跟entry > 8700
那样重复它们。最后,当您输入99999时,它将修复正在打印tax_owed = ..something like 140000..
的其他错误。
更改循环的条件(引入布尔变量)将更具可读性。
我根本不是税务人员,但您确定要添加tax_owed += entry;
吗?我的意思是,当我宣布10000美元时,我真的欠10130.5美元吗?在任何情况下,拥有1行而不是2行更具可读性:tax_owed = entry + 17442 * 0.28; // but do you really add 'entry'?
。
所以,代码将是这样的:
continueEntering = true;
while ( continueEntering ) {
tax_owed = 0;
entry = prompt("Enter taxable income as a valid number\n" + "Or enter 99999 to end entries", 99999);
entry = parseInt(entry);
if (entry == 99999) {
continueEntering = false;
} else if (entry < 0){
alert("Please enter a positive number");
} else if (entry <= 8700){
tax_owed = entry * 0.10;
} else if (entry <= 35350){
tax_owed = 870 * 0.15 + entry;
} else if (entry <= 85650){
tax_owed = 4867 * 0.25 + entry;
} else if (entry <= 178650){
tax_owed = 17442 * 0.28 + entry;
} else if (entry <= 388350){
tax_owed = 43482 * 0.33 + entry;
} else {
tax_owed = 112683 * 0.35 + entry;
}
alert("Tax owed is " + tax_owed);
}
如评论中所述,最好不要在循环体内使用值本身。如果这些值存储在变量中,那么更容易将这些值更改为一天。在这里你可以使用2个数组。你可以打电话给他们,比如thresholds[]
和tax_percentage[]
,或者其他什么(你比我知道的更好)。使用数组的好处是,您可以通过原始if
循环中的一个for
循环替换while
语句的序列。
================更新====================
以下是重构上述代码以使用for
循环代替大量if
的方法。
continueEntering = true;
while ( continueEntering ) {
tax_owed = 0;
exitValue = 99999;
tax_threshold = [ 0, 8700, 35350, 85650, 178650, 388350 ];
tax_rate = [ 0.10, 0.15, 0.25, 0.28, 0.33, 0.35 ];
additional_tax = [ 0, 870, 4867, 17442, 43482, 112683 ];
entry = prompt("Enter taxable income as a valid number\n" +
"Or enter " + exitValue + " to end entries", exitValue);
entry = parseInt(entry);
if (entry == exitValue) {
continueEntering = false;
} else if (entry < 0){
alert("Please enter a positive number");
} else {
for( i = tax_threshold.length-1; i >= 0; i-- ) {
if ( entry > tax_threshold[i] ) {
tax_owed = entry - tax_threshold[i];
tax_owed *= tax_rate[i];
tax_owed += additional_tax[i];
break;
}
}
}
alert("Tax owed is " + tax_owed.toFixed(2));
}
答案 3 :(得分:0)
好的,我想出了计算税收的方法。你们是对的。我离开了。我正在使用2012年的所得税政策,因为这是我的教授希望我们使用的。再次感谢大家的帮助。我真的很喜欢这个网站!非常适合我们n00bs。
var tax_owed = 0;
var entry;
continueEntering = true;
while (continueEntering) {
tax_owed = 0;
entry = prompt("Enter taxable income as a valid number\n" + "Or enter 99999 to end entries", 99999);
entry = parseInt(entry);
if (entry == 99999) {
continueEntering = false;
} else if (entry < 0){
alert("Please enter a positive number");
} else if (entry <= 8700){
tax_owed = entry * 0.10;
} else if (entry <= 35350){
tax_owed = entry - 8700;
tax_owed *= 0.15;
tax_owed += 870;
} else if (entry <= 85650){
tax_owed = entry - 35350;
tax_owed *= 0.25;
tax_owed += 4867;
} else if (entry <= 178650){
tax_owed = entry - 85650;
tax_owed *= 0.28;
tax_owed += 17442;
} else if (entry <= 388350){
tax_owed = entry - 178650;
tax_owed *= 0.33;
tax_owed += 43482;
} else if (entry > 388350){
tax_owed = entry - 388350;
tax_owed *= 0.35;
tax_owed += 112683;
}
alert("Tax owed is " + tax_owed.toFixed(2));
}