6 Replies - 734 Views - Last Post: 13 April 2013 - 05:19 PM

#1 jeffmerritt  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 12-April 13

Calculate formula conditional on entered value

Posted 12 April 2013 - 08:49 PM

I'm new to javascript and I apologize if this is really simple but I've been trying adapt code from forums and unfortunately it's just not working.

I want someone to enter their income and based on that value, I need to calculate the tax surcharge they'd face.

If they enter $500,000 or less, the answer should be $0

If they enter any number >$500,000, I need to execute the following formula to get the answer:

((income - 500000)*0.043)-((income - 500000)*0.03876))+18122

I'm going to attach my code below but I think I may be completely misunderstanding how to approach this.



<script language="javascript">

function convert (form)

if (form.income.value<500000.01);
{
  form.result.value = 0
  }

else
  {
  form.result.value = ((form.income.value - 500000) * 0.043) - ((form.income.value - 500000) * 0.03876)) + 18122;
  }

</script>


<form method="post">Annual income:<input name="income" onchange="convert(this.form)" type="text" value="">
<p><input name="calculate" onclick="convert(this.form)" type="button" value="calculate"></p>
<p>Amount of tax surcharge:<input name="result" type="text" value=""></p></form>



This post has been edited by Dormilich: 13 April 2013 - 06:00 AM
Reason for edit:: fixed typo


Is This A Good Question/Topic? 0
  • +

Replies To: Calculate formula conditional on entered value

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: Calculate formula conditional on entered value

Posted 13 April 2013 - 12:23 AM

open the error console, and check what messages are there.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: Calculate formula conditional on entered value

Posted 13 April 2013 - 03:48 AM

You might want to ensure you're operating on actual numbers via the parseFloat() function.
Was This Post Helpful? 0
  • +
  • -

#4 jeffmerritt  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 12-April 13

Re: Calculate formula conditional on entered value

Posted 13 April 2013 - 12:54 PM

Here are the 3 errors that I'm getting:

Uncaught SyntaxError: Unexpected token else

Uncaught ReferenceError: convert is not defined
onchange

Uncaught ReferenceError: convert is not defined
onclick

The webpage where this is posted is http://advocate.nyc.gov/taxday

Any advice is appreciated. Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: Calculate formula conditional on entered value

Posted 13 April 2013 - 01:06 PM

The first hint comes from "Unexpected token else", which means...something before that is not right. Like this line:

if (form.income.value<500000.01);


That semi-colon throws everything off, because now it's an empty (no-op) if statement, the next block executes always (unassociated with the if) and then the parser comes and finds an else out of nowhere.

Your entire function needs to be in {}s.

function myFunc()
{
   // Meat of function goes here
}

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: Calculate formula conditional on entered value

Posted 13 April 2013 - 01:29 PM

Here, obviously this isn't homework.

<html>
	<head>
		<script type="text/javascript">
		function convert (form)
		{
			// Remove whitespace
			var incomeText = form.income.value.replace(/^s+|\s+$/g, '');

			// If empty, this is wrong.
			if (incomeText.length === 0)
			{
				alert('Please enter a valid income.');
				return false;
			}

			// It's also wrong if the value is not a valid float.
			var income = parseFloat(incomeText);
			if (isNaN(income))
			{
				alert('Please enter a valid income.');
				return false;
			}

			if (income <= 500000.0)
			{
				form.result.value = 0;
			}
			else
			{
				var agi = income - 500000;
				var surcharge = (agi * 0.043) - (agi * 0.03876) + 18122;
				form.result.value = surcharge.toFixed(2);
			}
		}
		</script>
	</head>
	<body>
		<form method="post">
			<p>Annual income: $<input name="income" onchange="convert(this.form)" type="text" value=""></p>
			<p><input name="calculate" onclick="convert(this.form)" type="button" value="calculate"></p>
			<p>Amount of tax surcharge: $<input name="result" type="text" value=""></p>
		</form>
	</body>
</html>


Was This Post Helpful? 1
  • +
  • -

#7 jeffmerritt  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 12-April 13

Re: Calculate formula conditional on entered value

Posted 13 April 2013 - 05:19 PM

JackOfAllTrades-- Thank you so very much for your help with this. Now seeing your approach, it all makes a lot of sense. I would've been up all night trying to get to this point - thank you. This is part of a news story which will go to press tomorrow AM in New York related to tax day on Monday.

Best,
Jeff
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1