Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-13-2013, 03: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
moonbeam429 is an unknown quantity at this point
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] );
}
}
moonbeam429 is offline   Reply With Quote
Old 02-13-2013, 04:24 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 02-13-2013, 06: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
moonbeam429 is an unknown quantity at this point
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);
}

<input name="c5" style="background-color: #d6d6d6" onChange="totalpower()" />
<input name="c6" style="background-color: #d6d6d6" onChange="totalpower()" />
<input name="sum"  value="">
moonbeam429 is offline   Reply With Quote
Old 02-13-2013, 06:54 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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 07:06 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
moonbeam429 (02-13-2013)
Old 02-13-2013, 07: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
moonbeam429 is an unknown quantity at this point
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.
moonbeam429 is offline   Reply With Quote
Old 02-13-2013, 07:13 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by moonbeam429 View Post
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.
Philip M is offline   Reply With Quote
Old 02-13-2013, 07: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
moonbeam429 is an unknown quantity at this point
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.
moonbeam429 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 Jump


All times are GMT +1. The time now is 02:20 AM.


Advertisement
Log in to turn off these ads.