Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Need help with Calculating

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 02-13-2013, 04:42 PM PM User | #1 moonbeam429 New to the CF scene   Join Date: Feb 2013 Posts: 8 Thanks: 2 Thanked 0 Times in 0 Posts Need help with Calculating I have been working on this calculator and with help and alot of reading I still have not figured this thing out. I am trying to make a basic ROI Calculator and it works the way it should but now the totals are not working . From everything I have been reading I wonder if I should even use javascript or if I should use PHP. I know basic programming (I understand what I read) but don't really know that much. What I am trying to do is kind of similar to the idea of this ROI Calculator http://www.ledareus.com/index.php?ro...formation_id=7. Can anyone help me with why my totals are not working? Do I need to make a totally different function for them? Here is the code I have been working with but just get stuck at the same exact spot with the totals not working. ```function money( amt ) { var n = amt.toFixed(0); return n.replace(/(\d)(?=(\d{3})+\b)/g,'\$1,'); } function totalpower() { var f = document.form; var v = new Object(); v.sum = f.c5.value * f.c6.value; v.ledsum = f.e5.value * f.e6.value; v.kwh = v.sum * f.c8.value * f.c9.value / 1000; v.ledkwh = v.ledsum * f.e8.value * f.e9.value / 1000; v.sum1 = v.kwh * f.c11.value; v.ledsum1 = v.ledkwh * f.e11.value; v.e15 = 100000; v.lampyears = (f.c15.value / f.c9.value) / f.c8.value; v.ledlampyears = (v.e15 / f.e9.value) / f.e8.value; v.e17 = 0; v.avgcost = f.c17.value / v.lampyears; v.ledavgcost = v.e17 / v.ledlampyears; v.totalmain = (v.avgcost + f.c19.value); v.ledtotalmain = (v.ledavgcost + f.e19.value); v.totalancost = v.sum1 - 2 + v.totalmain; v.ledtotalancost = v.ledsum1 - 2 + v.ledtotalmain; v.totalpurprice = f.c25.value * f.c5.value; v.ledtotalpurprice = f.e25.value * f.e5.value; for ( var val in v ) { f[val].value = money( v[val] ); } }```
 02-13-2013, 05:24 PM PM User | #2 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,473 Thanks: 200 Thanked 2,469 Times in 2,447 Posts If you do not show the corresponding HTML there is no way we can help you. As it stands the code is meaningless. Have you tried using your error console (F12)? Have you tried placing alerts at strategic positions to inspect the values at that stage? What is the point of using an object var v = new Object(); here? We are in unprecedented territory, and we have been here before. Presenter, BBC Radio 2 __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.
 02-13-2013, 07:34 PM PM User | #3 moonbeam429 New to the CF scene   Join Date: Feb 2013 Posts: 8 Thanks: 2 Thanked 0 Times in 0 Posts I actually re-wrote the entire code and it works now. I am sure there is another way that is probably better and shorter but I am not an experienced coder. I am slowly learning. Now off to get the decimals, comma's and to get the other to round off is going to be difficult. Here is an example of the code I wrote so you can see what I was trying to do. Code: ```function totalpower(){ c5=formatinput(document.form.c5.value); document.form.c5.value=c5; e5=formatinput(document.form.e5.value); document.form.e5.value=e5; c6=formatinputfloat(document.form.c6.value); document.form.c6.value=c6; e6=formatinputfloat(document.form.e6.value); document.form.e6.value=e6; sum=(c5 * c6); document.form.sum.value=formatinput(sum); ledsum=(e5 * e6); document.form.ledsum.value=formatinput(ledsum); } ```
 02-13-2013, 07:54 PM PM User | #4 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,473 Thanks: 200 Thanked 2,469 Times in 2,447 Posts c5=formatinput(document.form.c5.value); It is very bad practice to use the same name such as c5 or sum for an HTML element and a Javascript variable. It leads to a lot of trouble. Also, you should use the var keyword to make your variables local to the function, and not global which is the case if the var keyword is omitted. Although the default of an input field is "text" you should still specify it. To round a display value to x decimal places, use .toFixed(x). Note that the value then becomes a string so cannot be used in any further arithmetic calculations. What is the point of your formatinput() function? You change a string value (as input by the user) into a number with Number() and trap NaN errors with var c5val = Number(document.form.c5.value) || 0; // value is 0 if the conversion to a number results in NaN Numbers by default are real (floating point). If you want an integer value use Math.floor() or Math.round(). Or block the entry of decimal points in the input field. If you want to perform your calculation onchange (not onChange), you must assign a default value of 0 to the two input fields. Otherwise NaN occurs. __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration. Last edited by Philip M; 02-13-2013 at 08:06 PM..
 Users who have thanked Philip M for this post: moonbeam429 (02-13-2013)
 02-13-2013, 08:03 PM PM User | #5 moonbeam429 New to the CF scene   Join Date: Feb 2013 Posts: 8 Thanks: 2 Thanked 0 Times in 0 Posts Thanks Phillip. This is just a basic calculator but it has me about crazy. I give major props to you coders. I am relatively new designer coming out of college who took on more than I should have (haha) but I for sure have learned from this calculator. Thanks for your help! I really do appreciate it.
02-13-2013, 08:13 PM   PM User | #6
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,473
Thanks: 200
Thanked 2,469 Times in 2,447 Posts
Quote:
 Originally Posted by moonbeam429 Thanks Phillip. This is just a basic calculator but it has me about crazy. I give major props to you coders. I am relatively new designer coming out of college who took on more than I should have (haha) but I for sure have learned from this calculator. Thanks for your help! I really do appreciate it.
Glad to be able to offer assistance. Be aware that you cannot expect to learn computer programming or Javascript in only a few weeks, or even a few months. But Good Luck!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

 02-13-2013, 08:23 PM PM User | #7 moonbeam429 New to the CF scene   Join Date: Feb 2013 Posts: 8 Thanks: 2 Thanked 0 Times in 0 Posts I took a JavaScript class in school but only one. After that everything seemed geared towards JQuery, then even less and less coding with such things as Drupal and WordPress...so as you can probably tell I haven't really coded alot, and the little bit that I did it was in a class room setting. I know basics and that is about as far as it goes.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 10:43 PM.