HighDots Forums  

Re: Help With A Calculation

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Help With A Calculation in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
KC Wong
 
Posts: n/a

Default Re: Help With A Calculation - 09-24-2003 , 04:22 AM






Quote:
I have written a form which collects input for a fare calculation. The
form
collects the following:
<snip assignment>

Quote:
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.

Quote:
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?

Quote:
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.

Quote:
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




Reply With Quote
  #2  
Old   
Michael Hagstrom
 
Posts: n/a

Default Re: Help With A Calculation - 09-24-2003 , 05:18 AM






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" <sterilize.the.spammers (AT) killkillkill (DOT) com> wrote

Quote:
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





Reply With Quote
  #3  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: Help With A Calculation - 09-24-2003 , 12:56 PM



JRS: In article <Laecb.22204$an.17894 (AT) bignews6 (DOT) bellsouth.net>, seen in
news:comp.lang.javascript, Michael Hagstrom <hagstr_m (AT) bellsouth (DOT) 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.

Quote:
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.

Quote:
Am I missing something obvious here?
Yes : the FAQ, including how to reply in News and other matters.


Quote:
"KC Wong" <sterilize.the.spammers (AT) killkillkill (DOT) com> wrote in message
news:bkrnp2$506v1$1 (AT) ID-200690 (DOT) 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.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.