Smurfling
Smurfling

Reputation: 99

How do I use a variable from another function without making the variable global in JavaScript?

Essentially, I have two different functions performing different calculations based on HTML form inputs.

I need a 3rd function to take one variable from each of them to perform the last math calculation.

Is there any way to do that without making the variables global or combining the functions?

Here are the functions:

function getStateTax(){
    var income = salaryBox.value;
    var sel = document.getElementById("state");
    var state = sel.options[sel.selectedIndex].value;
    var rate = taxRates[state];
    var el = document.getElementById("rate");
    el.innerHTML = rate;
    stateTaxElement.innerHTML = "Your state tax amount is: $" + Math.round(((income * (1 + rate)) - income) * 100) / 100;
}

function getFedTax() {
    var fedTax = 0;
    var income = salaryBox.value;
    if (document.getElementById("single").checked) {
        if (income <= 8350) {
            fedTax = income * .10;
        } else if (income <= 33950) {
            fedTax = 8350 * 0.10 + (income - 8350) * 0.15;
        } else if (income <= 82250) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
        } else if (income <= 171550) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;
        } else if (income <= 372950) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;
        } else {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;
        }
    }
    if (document.getElementById("jointorwidow").checked) {
        if (income <= 16700) {
            fedTax = income * 0.10;
        } else if (income <= 67900) {
            fedTax = 16700 * 0.10 + (income - 16700) * 0.15;
        } else if (income <= 137050) {
            fedTax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 137050) * 0.25;
        } else if (income <= 208850) {
            fedTax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;
        } else if (income <= 372950) {
            fedTax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;
        } else {
            fedTax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (income - 372950) * 0.35;
        } 
    }
    if (document.getElementById("separate").checked) {
        if (income <= 8350) {
            fedTax = income * 0.10;
        } else if (income <= 33950) {
            fedTax = 8350 * 0.10 + (income - 8350) * 0.15;
        } else if (income <= 68525) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
        } else if (income <= 104425) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;
        } else if (income <= 186475) {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;
        } else {
            fedTax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (income - 186475) * 0.35;
        }
    }  
    if (document.getElementById("head").checked) {
        if (income <= 11950) {
            fedTax = income * 0.10;
        } else if (income <= 45500) {
            fedTax = 11950 * 0.10 + (income - 11950) * 0.15;
        } else if (income <= 117450) {
            fedTax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
        } else if (income <= 190200) {
            fedTax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;
        } else if (income <= 372950) {
            fedTax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (income - 190200) * 0.33;
        } else {
            fedTax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33 + (income - 372950) * 0.35;
        }
    }
    fedTaxElement.innerHTML = "Your federal tax amount is: $" + Math.round(fedTax * 100) / 100;
}

What I need to do is make a third function and add the fedTax and first income variables together and subtract them from a new variable.

Upvotes: 0

Views: 74

Answers (1)

SidOfc
SidOfc

Reputation: 4594

Functions in javascript have the capability of returning values (as they do in most if not all programming languages).

This means that when a function is finished executing you can return a specific value.

A simple example of returning a result would be a simple add function:

var add = function(a, b) {
    return a + b;
}

var result = add(20, 30); // result is 50
console.log(result);

Ofcourse this is a bad example since there's the + operator that allows you to add numbers or concatenate strings but still, it shows what return does.

If you'd replace the return a + b; with just a + b; in that line then no value will be returned.

Now imagine we have a function that takes 3 parameters, the first two will be added onto each other and the third will be subtracted from the result.

We can use the add method nested within our function to return a result that we can continue our calculations with.

This function would look like this:

var custom_calc = function(a, b, c) {
    var add_res = add(a, b);
    return (add_res - c);
}

var result = custom_calc(10, 20, 20); // result is 10
console.log(result);

So in your first two functions, you'll want the last line of each function to be a return statement returning the variable that you need in the third function. In my example we used add to get a result, stored it in a variable and we used that variable to do further calculations and we return the result of that.

The same applies to your situation, good luck!

Upvotes: 1

Related Questions