Solved

Help me fix my budget calculator javascript?

Posted on 2013-01-05
12
439 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
  • 5
  • 4
  • 3
12 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
Comment Utility
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
Comment Utility
roopeshreddy that is irrelevant to the question. JS and CSS may be inline without influencing execution
0
 
LVL 23

Expert Comment

by:Roopesh Reddy
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now