功能不执行,一定是错误的

时间:2014-06-15 01:52:24

标签: javascript html function variables

我正在为课程做作业,我的代码工作很好,直到我调用函数getTaxRate。在此之前一切都很好,但是getTaxRate没有。感觉没有任何东西传递给函数,或者我输错了。但是我一直盯着这个,所以现在一切看起来都一样。所以,欢迎任何有用的评论。

<!doctype HTML>
<html>
<head>
<title> Matthew Young </title>
    <script language ="JavaScript">
    const RATE_ONE = .10;
    const RATE_TWO = .15;
    const RATE_THREE = .25;
    const RATE_FOUR = .28;
    const RATE_FIVE = .33;
    const RATE_SIX = .35;
    const RATE_SEVEN = .396;

function getTaxRate(inIncome) {
    var taxRate;
    var taxIncome = parseInt(inIncome);
    if (taxIncome >= 0 && taxIncome <= 8925)
    {
        taxRate = RATE_ONE;
    }
    if else (taxIncome >= 8926 && taxIncome <= 36250)
    {
        taxRate = RATE_TWO;
    }
    if else (taxIncome >= 36251 && taxIncome <= 87850)
    {
        taxRate = RATE_THREE;
    }
    if else (taxIncome >= 87851 && taxIncome <= 183250)
    {
        taxRate = RATE_FOUR;
    }
    if else (taxIncome >= 183251 && taxIncome <= 398350)
    {
        taxRate = RATE_FIVE;
    }
    if else (taxIncome >= 398351 && taxIncome <= 400000)
    {
        taxRate = RATE_SIX;
    }
    else (taxIncome >= 400001)
    {
        taxRate = RATE_SEVEN;
    }
    return taxRate;
}

function calculateTaxes(inTaxableIncome, inTaxRate) {
    var income = parseInt(inTaxableIncome);
    var rate = parseFloat(inTaxRate);
    var owed = intaxableIncome * inTaxRate;

    return owed;
}

function refundOrPay(inTaxesOwed, inTaxesPaid) {
    var owed = parseInt(inTaxesOwed);
    var paid = parseInt(inTaxesPaid);
    var taxDifference = owed - paid;

    return taxDifference;
}
    </script>
</head>

<body>
    <script language ="JavaScript">
        document.writeln("Welcome to COP 2500 Tax Services!");
        var taxIncome = prompt("Enter your year-end income: ");
        var deduction = prompt("Enter your taxable deduction: ");

        var taxableIncome = parseInt(taxIncome) - parseInt(deduction);  
        document.writeln("<br>Your taxable income is " + taxableIncome);

        var rate = getTaxRate(taxableIncome);
        document.writeln("Based on a taxable income of " +taxableIncome+ "your tax rate is " +rate * 100+ "percent<br>");


    </script>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

你在&#34; if else&#34;中有语法错误。如果&#34;你需要使用&#34;否则。更新您的功能,如下所示。

function getTaxRate(inIncome) {
var taxRate;
var taxIncome = parseInt(inIncome);
if (taxIncome >= 0 && taxIncome <= 8925)
{
    taxRate = RATE_ONE;
}
else if (taxIncome >= 8926 && taxIncome <= 36250)
{
    taxRate = RATE_TWO;
}
else if(taxIncome >= 36251 && taxIncome <= 87850)
{
    taxRate = RATE_THREE;
}
else if(taxIncome >= 87851 && taxIncome <= 183250)
{
    taxRate = RATE_FOUR;
}
else if(taxIncome >= 183251 && taxIncome <= 398350)
{
    taxRate = RATE_FIVE;
}
else if(taxIncome >= 398351 && taxIncome <= 400000)
{
    taxRate = RATE_SIX;
}
else (taxIncome >= 400001)
{
    taxRate = RATE_SEVEN;
}
return taxRate;
}

答案 1 :(得分:0)

另一个人通过重写它来为你修复它,但这是他为你的每个陈述所做的(但我使用了一些较少的换行符)。

你有很多人推荐调试器,所以我不需要。

但看起来所有的“if”和“else”语句都搞砸了, 他们应该看起来像这样(我喜欢最小化我的换行符):

    if (condition) {
        executable code;
    } else (a different condition) {
        you get the idea;
    }

有时候Javascript有点挑剔,但如果你有任何经验(我假设你正在学习),它本质上就像Ruby一样。

(我也会考虑将javascript作为未来的更复杂项目的单独文件)