2

this is an exercise from Murach's Javascript and Jquery book. 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. commented out if statement outputted the same 'undefined' error. The documentation said, undefined usually is outputted when a variable isn't assigned or returned and i've done all that.

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

<!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>
Ace
  • 59
  • 1
  • 7
  • You're mixing jQuery and plain JavaScript rather freely, and I believe it is confusing you. A jQuery object does not have a `value` property, nor will it find anything using the selector "income". Also, you haven't told us where you receive the error. – Heretic Monkey Feb 09 '18 at 22:04
  • Mike- This isn’t actually using jQuery. The mere is a closure function that is declared at the top that uses the $. Give me a minute to look through this. – andre mcgruder Feb 09 '18 at 22:36

1 Answers1

1

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;
Stradosphere
  • 1,285
  • 3
  • 14
  • 40
  • the .val() returned the same "undefined" output. The issue was simply logical errors with my math, for the calculation of the tax owed. Paranthesis were missing AND i only wrote the condition for when the income was > 9275 not if it was >= . – Ace Feb 10 '18 at 04:17