简体   繁体   English

所得税计算器的未定义输出。 JavaScript

[英]undefined output from Income Tax Calculator. JavaScript

this is an exercise from Murach's Javascript and Jquery book.这是 Murach 的 Javascript 和 Jquery 书中的一个练习。 An income tax calculator.所得税计算器。 The code is pretty self explanatory.代码是不言自明的。 The error: undefined is popping up where the value of the tax owed should be.错误:undefined 出现在应缴税款的位置。 commented out if statement outputted the same 'undefined' error.注释掉 if 语句输出相同的“未定义”错误。 The documentation said, undefined usually is outputted when a variable isn't assigned or returned and i've done all that.文档说,未定义通常在未分配或返回变量时输出,而我已经完成了所有这些。

JS JS

"use strict";
var $ = function (id) {
    return document.getElementById(id);
};

   var income;
   var taxOwed;
   var calculateTax = function(income){
    /*if (income > 0 && income < 9275){
        //incomeTax = taxableIncome - 0;
        //percentIT = incomeTax * (10/100);
        taxOwed = (income - 0) * (10/100) + 0;
        taxOwed = parseFloat(taxOwed);
        taxOwed = taxOwed.toFixed(2); //the tax should be rounded two 
        return taxOwed;
    }*/

    if (income < 9275){
        taxOwed = income - 0 * .10 + 0;
    }else if(income > 9275){
        taxOwed = ((income - 9275) * .15) + 927.50;
    }
    return taxOwed;

};

var processEntry = function(){
    income = parseInt($("income").value); //users entry should be converted 
    if(isNaN(income)){
        alert("Entry must be a number");
    }else if (income <= 0){
        alert("income must be greater than 0");
    }else{
        $("taxOwed").value = calculateTax(income);
    }
};  

window.onload = function () {
    $("calculate").onclick = processEntry;
    $("income").focus();
};

HTML HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Ace</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <h1>Income Tax Calculator</h1>

    <label>Enter taxable income:</label>
    <input type="text" id="income" /> <!--income is variable name for taxable income-->
    <input type="button" value="Calculate" name="calculate" id="calculate" /><br><br>

    <label>Income tax owed:</label>
    <input type="text" id="taxOwed"><br> <!--tax is variable name for taxOwed*-->
<script src="calculate_tax.js"></script>
</body>
</html>

You probably aren't passing anything in. put a debugger statement after income and open your dev console, check if the variable actually has a value.您可能没有传入任何内容。在收入之后放置一个调试器语句并打开您的开发控制台,检查该变量是否确实具有值。

//use val() not value
income = parseInt($("income").val());
debugger;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM