Hello and welcome to our community! Is this your first visit?
AdSlot7
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Need help with Calculating

1. ## 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] ); } }```

2. AdSlot6
3. 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

4. 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="">```

5. 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.

6. ## Users who have thanked Philip M for this post:

moonbeam429 (02-13-2013)

7. 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.

8. 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!

9. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
AdSlot4
AdSlot5