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
egoselfaxisAsked:
Who is Participating?
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
this helped


if(empty($('#in_checking_acct')) || empty($('#hourly_rate').val()) || empty($('#estimated_hours').val()) || empty($('#tax_percentage').val())) {
            
  alert("You must provide values for Checking Account, Hourly Rate, Estimated Hours, and Tax Percentage");
} else {                  
  var in_checking_acct = parseInt($('#in_checking_acct').val(),10);      
0
 
Michel PlungjanIT ExpertCommented:
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?
0
 
Roopesh ReddyIT AnalystCommented:
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...
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Michel PlungjanIT ExpertCommented:
roopeshreddy that is irrelevant to the question. JS and CSS may be inline without influencing execution
0
 
Roopesh ReddyIT AnalystCommented:
@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!
0
 
egoselfaxisAuthor Commented:
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
0
 
Michel PlungjanIT ExpertCommented:
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

0
 
egoselfaxisAuthor Commented:
>> var in_checking_acct = parseInt($('#in_checking_acct').val(),10);  

This revision is resulting in incorrect math calculations.

- Yvan
0
 
Michel PlungjanIT ExpertCommented:
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
0
 
Roopesh ReddyIT AnalystCommented:
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!
0
 
egoselfaxisAuthor Commented:
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
0
 
egoselfaxisAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.