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

#1 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • 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 contractsAdded = document.forms[0].contractsAdded.value;
	var priceAdded = document.forms[0].priceAdded.value;
	var contractsSold = document.forms[0].contractsSold.value;
	var priceSold = document.forms[0].priceSold.value;

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

	document.forms[0].totalAddedCost.value = 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

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

Posted 02 July 2011 - 03:00 AM

View Postdragon40226, 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)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1