?
Solved

Help me fix my budget calculator javascript?

Posted on 2013-01-05
12
Medium Priority
?
500 Views
Last Modified: 2013-01-13
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
0
Comment
Question by:egoselfaxis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38748089
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
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38748095
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38748107
roopeshreddy that is irrelevant to the question. JS and CSS may be inline without influencing execution
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38749795
@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
 

Author Comment

by:egoselfaxis
ID: 38760091
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 38766261
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38766263
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
 

Author Comment

by:egoselfaxis
ID: 38768308
>> var in_checking_acct = parseInt($('#in_checking_acct').val(),10);  

This revision is resulting in incorrect math calculations.

- Yvan
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38768835
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
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38769397
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
 

Author Comment

by:egoselfaxis
ID: 38771909
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
 

Author Comment

by:egoselfaxis
ID: 38771915
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question