473,385 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Javascript calculation help

Hi guys,

I have run into a problem and I can't figure out what's happening or why this script won't work. I am a self-taught scripter and only know what I use really. Any help would be appreciated. OK, here's the problem:

I have a total of 5 dollar fields that will be calculated into a 6th dollar field. An example of what I need is like -------- field1 + field2 + field3 + field4 * field5. The answer to all of that gets put into the 6th field, which will also need to be rounded to the nearest whole $. The only thing I want rounded is the answer, not the individual fields themselves. Also, one thing that I know for sure is wrong (and something I don't know how to do) is the fact that all the fields are optional. Meaning, maybe there will be input in them, and maybe there won't. I just have no idea how to script that. Here's what I've got that isn't working--------------

Expand|Select|Wrap|Line Numbers
  1. var a = this.getField("LO");
  2. var b = this.getField("PP");
  3. var c = this.getField("AEC");
  4. var d = this.getField("UM");
  5. var e = this.getField("TOT");
  6. event.value = Math.round(a.value+b.value+c.value+d.value*e.value);
  7.  
I have tried putting the individual values into ( ) by themselves, which doesn't make a difference. ----------One thing that I noticed that was happening is that when you only enter a value in the first 2 fields, it would get multiplied by 10 for some reason. Then the 3rd field would bring it back to the correct answer, and then the 4th wouldn't have any effect. (Using the value of 1 in all fields.) So 1+1 would show, a total of 20. 1+1+1 would be 3. 1+1+1+1 would be 3. ---------- Any help would be appreciated guys. Thank you!!!
Nov 1 '07 #1
3 1247
iam_clint
1,208 Expert 1GB
I would do something like this.
Expand|Select|Wrap|Line Numbers
  1. var a = this.getField("LO");
  2. var b = this.getField("PP");
  3. var c = this.getField("AEC");
  4. var d = this.getField("UM");
  5. var e = this.getField("TOT");
  6. if (a=="") { a = 0; }
  7. if (b=="") { b = 0; }
  8. if (c=="") { c = 0; }
  9. if (d=="") { d = 0; }
  10. if (e=="") { e = 0; }
  11. var answer = Math.floor(a.value+b.value+c.value+d.value*e.value);
  12. //var answer = Math.floor(a.value+b.value+c.value+d.value)*e.value; //depends on how you want it calculated a.value+b.value+c.value+d.value = blah * e.value is how this is your other one could be interpreted d.value*e.value+c.value+b.value+a.value
  13. event.value = Math.round(answer);
  14.  
Nov 1 '07 #2
Thanks iam_clint. That pushed me in the right direction. Response very much appreciated!!! Looks like I'll meet my deadline! : )

So to use the if statement to make a field equal 0.00 if there is no input, would I have to name the variable the field I am working with and then use the if statement you gave me? Example:

Expand|Select|Wrap|Line Numbers
  1.  
  2. var a = this.getField("LO");
  3. if (a=="") { a = 0.00; }
  4.  
  5.  
Do I understand you correctly?
Nov 1 '07 #3
iam_clint
1,208 Expert 1GB
yes, also 0.00 is the same as just 0.

And you are putting a space [ /CODE] like this you need to take that space out for posting here.
Nov 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
3
by: Sean McCourt | last post by:
Hi I am doing a JavaScript course and learning from the recommed book (JavaScript 3rd Edition by Don Gosslin) Below is one of the exercises from the book. I get this error message when I try to...
0
by: anaxamandr | last post by:
Hi. I have a long loop in ASP that performs a rather lengthy calculation. I would love for my users to be able to stop that calculation, if they so choose, mid way through the process. I attempted...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
2
by: Del | last post by:
Thanks in advance for any help. I have a database that was created in Access 2000. Several users have been upgraded to Access 2003. Since upgrading to 2003 we have noticed that some of the...
4
by: Michiel Alsters | last post by:
Hello everybody, I hope anybody can help me. I'll try to give a brief overview of my problem. I have running a program that performs a heavy calculation. To give the user feedback what the...
7
by: google | last post by:
I would like to execute a single JavaScript command from within a web service. The command is a mathmatical statement. I have a web page with some client side logic that does this and I'm now...
1
by: theflyingminstrel | last post by:
Hi, I’m having some trouble with a Javascript code, and I was wondering if anyone can help: I am trying to build a price estimator that has multiple fields. I would like the first two fields to...
4
by: =?Utf-8?B?R1ROMTcwNzc3?= | last post by:
Hi Guys, thanks for your help yesterday, I've got one more question, then I think I'm done for now,... Is it possible to insert recordset data in a javascript, for instance I have a javascript...
10
by: Jon Harrop | last post by:
What is the shortest and simplest Javascript implementation and where can I find a BNF for this language? -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/?u
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.