Solved

Help me fix my budget calculator javascript?

Posted on 2013-01-05
12
466 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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
w3c parsing errors 4 36
NOT having JQuery DataTable Columns Sort 7 35
Javascript Error 10 21
Add Promise to Existing Code 6 26
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article discusses four methods for overlaying images in a container on a web page
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

737 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