Avatar of egoselfaxis
egoselfaxis
 asked on

Help me fix my budget calculator javascript?

I built this budget calculator using HTML, CSS & Javascript / JQuery a while back, and am wondering if someone here might be able to help me fix my javascript so that it's more efficient with adding up and calculating dollar amounts.  

http://budget.egoselfaxis.com/

I'm a subcontractor, and I use this app to help keep track of my tax-adjusted income vs. expenses.  Once you establish an "economic forecast" by filling in the 4 initial text input fields, you're able to add up individual expenses, .. drag and drop them to new positions (if necessary), .. and then delete each expense once it's been paid off. The result is a "tape roll" like summary of where you're at financially (meaning an estimate of how much extra spending money you should have left over if everything works out as planned).

Anyways, -- my javascript-based math functions are buggy, and sometimes result in obviously wrong and enormous amounts.  I think part of the problem is that I'm purposefully excluding the penny values from some of my calculations, and have to accommodate for them programmatically as needed.  Obviously I'm doing this incorrectly. What I'd like to do is have someone here help me fix it.  Anyone care to lend a hand?  

Feel free to bookmark this link if you find it useful in any way, by the way. You'll see that it uses HTML5's local storage functions, and doesn't require any server-side scripting.  In fact -- you can just save it down to your local machine and to run it from there if you prefer.  

Thanks, -- and happy new year!

- Yvan
JavaScriptHTMLjQuery

Avatar of undefined
Last Comment
egoselfaxis

8/22/2022 - Mon
Michel Plungjan

1) add empty($('#in_checking_acct').val()) to the validation. It is very ugly when you don't
2) change

$('.budget_item').each(function(){
  total = total  + parseInt($(this).text());                              
});      

to

$('.budget_item').each(function(){
  var val =  $(this).text();
  if (!val || isNaN(val)) val =0;
  else val = parseInt(val,10);
  total += val;                              
});      

Can you give a step by step to recreate an error?
Roopesh Reddy

Hi,

You have mixed up everything(CSS and JavaScript code) in one file, which should be separated!

Put all the Styles in the separate .CSS file and refer it in your HTML file like this -

<link href="Site.css" rel="stylesheet" type="text/css" />

Open in new window


Same way with JavaScript too -
<script src="MyScript.js" type="text/javascript"></script>

Open in new window


Once you are done with that, get back to us and will check the code! Moreover, show us the functions we need to check instead of all which may take time!

Hope it helps u...
Michel Plungjan

roopeshreddy that is irrelevant to the question. JS and CSS may be inline without influencing execution
Your help has saved me hundreds of hours of internet surfing.
fblack61
Roopesh Reddy

@mplungjan

Yeah, Correct. I just mentioned the author about the same and asked him to make thing clear and point us to the functionality which he requires refactoring of the code!

Do correct me, if i'm wrong!

Thanks!
egoselfaxis

ASKER
To recreate the error .. enter a value of "100" or "1000" (without the quotes) into the "Estimated Hours" text input field, and then click on the red "+" button that's to the right of it.  It seems that these types of values always result in the wrong calculations.

By the way .. although the edits you suggested didn't appear to break anything .. they don't seem to be making any difference.  

Thanks,
- Yvan
ASKER CERTIFIED SOLUTION
Michel Plungjan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Michel Plungjan

And this is easier to read:

var income_summary_html = "<h2>Income</h2>";
			
income_summary_html += '<p><strong>Total Dollar Amount Already In Checking Account:</strong> <div class="income">+ ' + formatDollar(in_checking_acct) + '</div></p><hr class="clear">';	
			
income_summary_html += '<p><strong>Total Projected Income Before Taxes:</strong> <div class="income">+ ' + formatDollar(income_before_taxes) + '</div></p><hr class="clear">';			

income_summary_html += '<p><strong>&nbsp;</strong> <div class="total">' + formatDollar(total_adjusted_income) + '</div></p><hr class="clear">';	
			
income_summary_html += '<p><strong>Minus This Month\'s IRS Tax Contribution</strong> <div class="expense">- ' + formatDollar(tax_contribution) + '</div></p><hr class="clear">';		

income_summary_html += '<p><strong>&nbsp;</strong> <div class="total">' + formatDollar(income_after_taxes) + '</div></p><hr class="clear">';					
			
var total = 0,val;
$('.budget_item').each(function(){
  val = $(this).text();
  if (!val || isNaN(val)) val =0;
  else val = parseInt(val,10);
  total += val;                              
}); 		

income_summary_html += '<p><strong>Minus Total Expenses For This Month:</strong> <div class="expense">- ' + formatDollar(total) + '</div></p><hr class="clear">';			

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
egoselfaxis

ASKER
>> var in_checking_acct = parseInt($('#in_checking_acct').val(),10);  

This revision is resulting in incorrect math calculations.

- Yvan
Michel Plungjan

Better than 19 million it did before...

Please elaborate. Perhaps there are more issues, I can just tell you now that before it was "1000"+100 which is 1000100
Roopesh Reddy

Hi,

I didn't see issues with the code provided by @mplungjan!

What type of functionality you are expecting? It's better to show the function which needs to be refactored and also the desired functionality to better assist you!

Waiting for your input!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
egoselfaxis

ASKER
After doing a bit more testing, .. the problem appears to only be happening when an "Approximate Dollar Amount Currently In Checking Account" value other than "0" is specified.

http://budget.egoselfaxis.com/

- Yvan
egoselfaxis

ASKER
Aha --- I changed this line ...

var spending_money_left_over = (income_after_taxes + in_checking_acct) - total;

to this ...

var spending_money_left_over = (income_after_taxes) - total;

.. and it now appears to be working correctly.

Thanks!
- Yvan