# Looking for some finese in my calculating/populating function

Page 1 of 1

## 4 Replies - 794 Views - Last Post: 02 July 2011 - 03:00 AM

### #1 teahou

Reputation: 6
• Posts: 224
• Joined: 01-April 11

# Looking for some finese in my calculating/populating function

Posted 01 July 2011 - 09:30 PM

Ok, I have 2 things going on here. One, the 'percent' variable is constantly showing up as NaN (not a number) no matter what numbers I give it. I suspect it is happening because the function is populating all the fields at one, which causes a 'divide by zero' scenario for the percentage field, which is last in line.

Which brings me to my send thing. I would like some ideas as to how to write this function correctly, as it obviously is causing problems as is, and it seems clunky. I think it should broken up into a couple differnet functions, but I tried that and NOTHING worked.

Here is the code, let me kow what you think:

```function calculate()
{
var contractsSold = document.forms[0].contractsSold.value;
var priceSold = document.forms[0].priceSold.value;

var totalSold = (contractsSold * priceSold) * 100;
var gainLoss = totalSold - totalAdded;
var percent = gainLoss / totalAdded;

document.forms[0].totalSoldCost.value = totalSold;
document.forms[0].gainLoss.value = gainLoss;
document.forms[0].returnPercent.value = percent;
}

```

Is This A Good Question/Topic? 0

## Replies To: Looking for some finese in my calculating/populating function

### #2 dragon40226

Reputation: 0
• Posts: 34
• Joined: 29-June 11

## Re: Looking for some finese in my calculating/populating function

Posted 01 July 2011 - 09:53 PM

Is there a such thing as a double or a float in javascript, i honestly dont know javascript but i know java.

### #3 teahou

Reputation: 6
• Posts: 224
• Joined: 01-April 11

## Re: Looking for some finese in my calculating/populating function

Posted 02 July 2011 - 12:17 AM

javascript is loosely-typed, so no need to declare a data type. Java and Javascript are as similar as grass and cell phones.

• Untitled

Reputation: 209
• Posts: 2,290
• Joined: 12-March 09

## Re: Looking for some finese in my calculating/populating function

Posted 02 July 2011 - 02:03 AM

try to use name of the form document.forms[Form Name]

or you can use document.forms.formname.

and it would be better to past your html code also.

### #5 Dormilich

• 痛覚残留

Reputation: 4208
• Posts: 13,283
• Joined: 08-June 10

## Re: Looking for some finese in my calculating/populating function

Posted 02 July 2011 - 03:00 AM

dragon40226, on 02 July 2011 - 06:53 AM, said:

Is there a such thing as a double or a float in javascript, i honestly dont know javascript but i know java.

Javascript exclusively uses IEEE 754 Floats for its numbers. (i.e. Javascript Integers are also Floats)