473,774 Members | 2,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript calculation help

6 New Member
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 1265
iam_clint
1,208 Recognized Expert Top Contributor
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
dcnorman07
6 New Member
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 Recognized Expert Top Contributor
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
5745
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 difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
3
2570
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 use the calculator. "document.Calculate.Input is null or not an object" Can someone please tell me why this is?
0
2482
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 to use a parent window that allows the user to launch the calculation in a seperate window, so that they could still click "stop" to write a value to a database or set a session level variable that the calculation would check to see if it should...
136
9456
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
2
3914
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 calculated fields are not being populated. The database is a samll invoicing database on the form and report we have columns call Unit Cost. This is the actual cost of the part and is pulled from our parts master table and is displayed in a sub form...
4
3277
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 program is doing I show a window which contains a progress bar and a label. At some point during the execution the state of the calculation is changed, so I want to let the user know this. I have placed the creation of the form in a seperate thread...
7
2109
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 moving that functionality into a web service. To clarify - on the UI the user is collecting and entering data and then it gets stored into the database. Most fields are input but there are some that are calculated and stored. The calculated...
1
1848
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 have a price value based on a quantity price, so for example 1-10 qantity equals $30, 20-30 quantity equals $40 in the “Total” field). The rest of the fields in the following code work as intended whereas they just add based on the price to the right....
4
2716
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 code that calculates the total price, depending on number of units, currently what the code does is set the price like so - if qty 1 then £99+VAT if qty equall to or greater than 2 and equall to or less than 9 then price =
10
2223
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
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.