473,406 Members | 2,356 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,406 software developers and data experts.

Help With A Calculation

Good Day -

I have written a form which collects input for a fare calculation. The form
collects the following:

1) Departure or Destination Group (1,2,or 3)
2) Number of Adults
3) Number of Children aged 4 to 12
4) Number of Children under age 4

Based on this data, I need to calculate a fare as follows:

1) The base fare is $80.00, $70.00 or $60.00 per person, depending on which
Departure/Destination Group is selected. If there is more than one adult,
all adults after the first one receive a 40% discount.

2) The rate per Child 4 - 12 is one have the base fare per child.

3) Children under 4 are free.

The following is the form and the Javascript, but I cannot get it to work.

Any help or suggestions would be truly appreciated.

Mike Hagstrom

************************************************** **************************
*
<form name="quote">
<table width="500" border="2" cellspacing="2" cellpadding="2"
align="CENTER">

<tr><td>Select Your Group:&nbsp;</td><td>Key West Group&nbsp;<input
type="Radio" name="Group" value="1"><br>Lower Keys Group&nbsp;<input
type="Radio" name="Group" value="2"><br>Middle/Upper Keys Group&nbsp;<input
type="Radio" name="Group" value="3"></td></tr>

<tr><td>Number of Adults:&nbsp;</td><td><INPUT NAME="Adults" SIZE=5
MAXLENGTH=50></td></tr>

<tr><td>Number Children 4-12 yrs:&nbsp;</td><td><INPUT NAME="Chil412"
SIZE=5 MAXLENGTH=50></td></tr>

<tr><td>Number Children Under 4 yrs:&nbsp;</td><td><INPUT NAME="ChilU4"
SIZE=5 MAXLENGTH=50></td></tr>

<tr><td ><input type=button value="Calculate"
onClick="calculate()"></td></tr>

<tr><td>Total Passengers:</td><td><input type=text name=totalpass
size=12></td></tr>

<tr><td>Your Fare:</td><td><input type=text name=totalfare
size=12></td></tr>
</table>
</form>
<script type=text/javascript>
function calculate() {
if (document.quote.Group.value = 1)
then Var farepp = 80;
else if (document.quote.Group.value = 2)
then Var farepp = 70;
else Var farepp = 60;

var Adults = document.quote.Adults.value;
var Chil412 = document.quote.Chil412.value;
var ChilU4 = document.quote.ChilU4.value;
var totalpass = Math.pow(Adults+Chil412+ChiU4);
var totalfare = Math.pow((farepp*Adults)+(Chil412*farepp/2));
document.quote.totalpass.value="";
document.quote.totalfare.value="";

}

</script>
Jul 20 '05 #1
5 3306
> I have written a form which collects input for a fare calculation. The
form
collects the following:
<snip assignment>
The following is the form and the Javascript, but I cannot get it to work.
That's not good enough... next time state clearly:
1. What results do you expect
2. What incorrect results you're getting

I'll give you some hints below.
<script type=text/javascript>
function calculate() {
if (document.quote.Group.value = 1)
then Var farepp = 80;
else if (document.quote.Group.value = 2)
then Var farepp = 70;
else Var farepp = 60;
1. JavaScript does not have a "then" keyword.
2. JavaScript, while loosely typed, are case-sensitive.
3. Scope rules: What scope do you want fareapp to be in?
var Adults = document.quote.Adults.value;
var Chil412 = document.quote.Chil412.value;
var ChilU4 = document.quote.ChilU4.value;
var totalpass = Math.pow(Adults+Chil412+ChiU4);
var totalfare = Math.pow((farepp*Adults)+(Chil412*farepp/2));
There's a typo in one of the above lines... should be obvious when you see
the error message.
document.quote.totalpass.value="";
document.quote.totalfare.value="";
}
</script>


You want to display the results, right? Then you shouldn't assign empty
strings to the fields here.

BTW the calculation is incorrect too... results in NaN. I'll leave that part
for you to fix... it looks like a "squish-the-bugs" kind of assignment to
me.
KC
Jul 20 '05 #2
KC -

Thanks for your input.

Based on your comments, I edited the script to remove the conditionals on
the "farepp" and set it static at 80. I've set the math to only calculate
the full fare without a discount.

I also cleaned up the typing and now I receive no error messages, but the
script returns nothing.

Here's the script as modified:
**********************************************
<script type=text/javascript>
function calculate() {

var farepp = 80;
var Adults = document.quote.Adults.value;
var Chil412 = document.quote.Chil412.value;
var ChilU4 = document.quote.ChilU4.value;
var totalfare = Math.pow((farepp*Adults)+(farepp*Chil412/2));
var totalpass = Math.pow(Adults+Chil412+ChilU4);

if (!isNaN(totalfare) &&
(totalfare != Number.POSITIVE_INFINITY) &&
(totalfare != Number.NEGATIVE_INFINITY)) {
document.quote.totalfare.value=Math.round(totalfar e);
document.quote.totalpass.value=Math.round(totalpas s);
}
else {
document.quote.totalfare.value="";
document.quote.totalpass.value="";
}

}
function round(totalfare) {return Math.round(totalfare *100)/100;}
</script>
************************************************** **************************
*
Am I missing something obvious here?

Thanks for any more help or suggestions.

Mike

"KC Wong" <st********************@killkillkill.com> wrote in message
news:bk************@ID-200690.news.uni-berlin.de...
I have written a form which collects input for a fare calculation. The form
collects the following:


<snip assignment>
The following is the form and the Javascript, but I cannot get it to

work.
That's not good enough... next time state clearly:
1. What results do you expect
2. What incorrect results you're getting

I'll give you some hints below.
<script type=text/javascript>
function calculate() {
if (document.quote.Group.value = 1)
then Var farepp = 80;
else if (document.quote.Group.value = 2)
then Var farepp = 70;
else Var farepp = 60;
1. JavaScript does not have a "then" keyword.
2. JavaScript, while loosely typed, are case-sensitive.
3. Scope rules: What scope do you want fareapp to be in?
var Adults = document.quote.Adults.value;
var Chil412 = document.quote.Chil412.value;
var ChilU4 = document.quote.ChilU4.value;
var totalpass = Math.pow(Adults+Chil412+ChiU4);
var totalfare = Math.pow((farepp*Adults)+(Chil412*farepp/2));


There's a typo in one of the above lines... should be obvious when you see
the error message.
document.quote.totalpass.value="";
document.quote.totalfare.value="";
}
</script>


You want to display the results, right? Then you shouldn't assign empty
strings to the fields here.

BTW the calculation is incorrect too... results in NaN. I'll leave that

part for you to fix... it looks like a "squish-the-bugs" kind of assignment to
me.
KC

Jul 20 '05 #3
JRS: In article <La******************@bignews6.bellsouth.net>, seen in
news:comp.lang.javascript, Michael Hagstrom <ha******@bellsouth.net>
posted at Wed, 24 Sep 2003 06:18:21 :-

Before writing programs using maths, learn some math; before writing
code using a system function, discover what it does.
var totalfare = Math.pow((farepp*Adults)+(farepp*Chil412/2));
var totalpass = Math.pow(Adults+Chil412+ChilU4); function round(totalfare) {return Math.round(totalfare *100)/100;}
That rounds a number to a number. You will want to round a number to a
string with two decimals.
Am I missing something obvious here?
Yes : the FAQ, including how to reply in News and other matters.

"KC Wong" <st********************@killkillkill.com> wrote in message
news:bk************@ID-200690.news.uni-berlin.de...

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4
Thanks for all your input -

I got the script working fine.

Sorry about using the Math.pow system function - I had taken it from another
piece of code I had written earlier, and forgot to remove it from my
example.

Thank you again.

Oh, yes - my math is just fine, and my name is
Dr. Michael Hagstrom - Catholic University of America, 1969

"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:fF**************@merlyn.demon.co.uk...
JRS: In article <La******************@bignews6.bellsouth.net>, seen in
news:comp.lang.javascript, Michael Hagstrom <ha******@bellsouth.net>
posted at Wed, 24 Sep 2003 06:18:21 :-

Before writing programs using maths, learn some math; before writing
code using a system function, discover what it does.
var totalfare = Math.pow((farepp*Adults)+(farepp*Chil412/2));
var totalpass = Math.pow(Adults+Chil412+ChilU4);
function round(totalfare) {return Math.round(totalfare *100)/100;}


That rounds a number to a number. You will want to round a number to a
string with two decimals.
Am I missing something obvious here?


Yes : the FAQ, including how to reply in News and other matters.

"KC Wong" <st********************@killkillkill.com> wrote in message
news:bk************@ID-200690.news.uni-berlin.de...

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE

4 © <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript <URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics,

links.
Jul 20 '05 #5
JRS: In article <ql******************@bignews6.bellsouth.net>, seen in
news:comp.lang.javascript, Michael Hagstrom <ha******@bellsouth.net>
posted at Fri, 26 Sep 2003 17:39:58 :-
Oh, yes - my math is just fine, and my name is
Dr. Michael Hagstrom - Catholic University of America, 1969
You seem to be a slow learner. Read the newsgroup FAQ, and try to learn
about the proper, considerate formatting of news replies. If that's not
enough, see the references below.
"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:fF**************@merlyn.demon.co.uk...
JRS: In article <La******************@bignews6.bellsouth.net>, seen in
news:comp.lang.javascript, Michael Hagstrom <ha******@bellsouth.net>
posted at Wed, 24 Sep 2003 06:18:21 :-
>Am I missing something obvious here?


Yes : the FAQ, including how to reply in News and other matters. --
© John
...


--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME ©
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Umesh | last post by:
Hi I am developing a windows application. This contains a Data grid which will populate data from a data table in a Dataset. The application will do some calculation and store the result in the...
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: leavandor | last post by:
I am trying to design a query that works with a relationship between a Table and a Query. I am comparing a value in the table with a computed value inside the query. The reason for this is that...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
1
by: jlf | last post by:
Need serious help with a calculation. The fields are based on a query that has linked many different tables. The calculation I currently have is: =sum()/((+)*(Piecesnum])) It is giving a...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
10
by: 60325 | last post by:
This is the page where I collect the data in drop-down boxes with values of 1-10 and send it to a submitted page to do calculations. Example: Employee1 TeamScore(1-10) Employee2 ...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
3
by: Dew | last post by:
Hello, iam new to programming, and ive been given a task in my Uni to complete certain exercises but i found it very difficulty to solve them. I was wandering if there is someone who can help me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.