在这段javascript代码中找不到错误

can't find error in this javascript code

本文关键字:找不到 错误 代码 javascript      更新时间:2023-09-26

它甚至没有运行。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));

}