在这段javascript代码中找不到错误
can't find error in this javascript code
它甚至没有运行。do while循环有问题。我想应该是有if语句的吧?如果有人能帮我,我将不胜感激。谢谢。
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);
在< 178651
之前丢失操作数的错误已经被发现,但是如果你想让你的代码有更少的潜在错误,我建议一个轻重构过程。
-
使用
else if
将更有效。一旦发现与给定entry
值对应的块,程序将停止检查其他条件。此外,您必须指定那些划分范围的值(您的"里程碑"),而不是像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[]
,或者其他什么(你比我更了解)。使用数组的好处是,您将能够在原始的while
循环中只使用一个for
循环来替换if
语句的序列。
================ 更新 ====================
下面是你如何重构上面的代码来使用一个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));
}
第21行
if (entry > 85650 && < 178651){ // there is no operand between && and <.
我认为应该是
if (entry > 85650 && entry< 178651){
在if (entry > 85650 && < 178651)
行,如果您仔细观察将会发现您没有将第二个数字与任何内容进行比较。应该是if (entry > 85650 && entry < 178651)
好了,我找到计算税款的正确方法了。你们是对的。我大错特错了。我用的是2012年的所得税政策,因为这是我的教授让我们用的。再次感谢大家的帮助。我真的很喜欢这个网站!对我们来说太好了。
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));
}
- SyntaxError:意外的标记else(我在代码中找不到错误)
- 精灵与身体错位,可以'在显示循环中找不到错误
- 可以'找不到错误
- 意外令牌 } 找不到错误
- 内部链接返回空错误,我在 Javascript 中找不到错误.外部链接工作得很好
- 我在脚本中找不到错误
- Firebug 不显示我的 JavaScript?找不到错误
- 表单不断抛出“不允许直接访问此页面.” 找不到错误
- Parse.com 找不到错误代码:141 函数
- 为什么使用 jQuery .replace 会导致网页找不到错误
- 在 html 模板中找不到错误.在非法令牌中解决)
- 找不到错误 - 发射次数太多
- JQuery 幻灯片错误.图片在页面上列出,而不是随机播放.找不到错误
- 检查是否输入了文本.找不到错误
- 可以'我在javascript代码中找不到错误
- 在这段javascript代码中找不到错误
- 语法错误找不到错误
- 在序列化列中找不到错误
- backbone.js可以'在脚本中找不到错误
- Javascript,找不到错误