user2108839
user2108839

Reputation: 17

3 returns for one input for a calculator

I am very new to programming so please forgive my perhaps dumb question. I have built three functions that calculate tax rates based upon the same input "amount". I am trying to figure out a way that I could have a user enter in the amount once and get the return from all three functions. Here they are below.

//Function 1
var normalrtfCalculator = function (amount) {
  if (amount <= 150000) {
    return Math.ceil(amount / 500) * 2;
  } else if (amount <= 350000) {
    if ((amount - 150000) <= 50000) {
      return 600 + (Math.ceil((amount - 150000) / 500) * 3.35);
    } else {
      return 935 + (Math.ceil((amount - 200000) / 500) * 3.9);
    }
  } else {
    if ((amount - 200000) <= 350000) {
      return 2735 + (Math.ceil((amount - 200000) / 500) * 4.8);
    } else if ((amount - 550000) <= 300000) {
      return 4655 + (Math.ceil((amount - 555000) / 500) * 5.3);
    } else if ((amount - 850000) <= 150000) {
      return 7835 + (Math.ceil((amount - 850000) / 500) * 5.8);
    } else {
      return 9575 + (Math.ceil((amount - 1000000) / 500) * 6.05);
    }
  }
};
//Function 2
var mansionTax = function (amount) {
  if (amount > 1000000) {
    return amount * 0.01;
  }
};
//Function 3
var lowincomertfCalculator = function (amount) {
  if (amount <= 350000) {
    if (amount <= 150000) {
      return (Math.ceil(amount / 500)) * 0.5;
    } else {
      return 150 + (Math.ceil((amount - 150000) / 500)) * 1.25;
    }
  } else {
    if ((amount - 150000) <= 400000) {
      return 420 + (Math.ceil((amount - 150000) / 500) * 2.15);
    } else if ((amount - 550000) <= 300000) {
      return 2140 + (Math.ceil((amount - 550000) / 500) * 2.65);
    } else if ((amount - 850000) <= 150000) {
      return 3730 + (Math.ceil((amount - 850000) / 500) * 3.15);
    } else {
      return 4675 + (Math.ceil((amount - 1000000) / 500) * 3.4);
    }
  }
};

Upvotes: 0

Views: 88

Answers (2)

loxxy
loxxy

Reputation: 13151

The minimum change required could be as little as adding () before assigning to the variables.

Something like :

 var amount = 100;

 //Function 2
 var mansionTax = (function () {
     if (amount > 1000000) {
         return amount * 0.01;
     }
 })();

Here is a working sample.

This will remove the need for defining any additional functions.

Upvotes: 0

Coin_op
Coin_op

Reputation: 10728

Just have a function that runs the other ones and returns an object of the results:

var calculateTax = function(amount){
  return {
    rtf : normalrtfCalculator(amount),
    mansion: mansionTax(amnount),
    lowincome: lowincomertfCalculator(amount)
  }
}

then you can call it like this:

var tax = calculateTax(99999);

To get the individual results you then just access the properties:

alert(tax.rtf), alert(tax.mansion) and alert(tax.lowincome)

Upvotes: 1

Related Questions