HighDots Forums  

IF & math formula student question

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss IF & math formula student question in the JavaScript discussion (multi-lingual) forum.



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

Default IF & math formula student question - 08-29-2005 , 10:05 PM






I'm using the formula:
var
ship=round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20)*3)
+2)+((total7/20)*2)),2)
to calculate shipping charges, it works perfect. Now, I want to modify this
formula so the minimum net result = 5 and the maximum = 30.

So if the calculated value equals less than 5, the result will be 5. If
exceeds 30, result will be 30.

I have tried different IF , <, > test permutations without success.

Any suggestions appreciated




Reply With Quote
  #2  
Old   
Kimmo Laine
 
Posts: n/a

Default Re: IF & math formula student question - 08-29-2005 , 10:49 PM






"Edward" <nomail (AT) yahoo (DOT) com> kirjoitti
viestissä:g2QQe.8923$Yh6.7258 (AT) fe04 (DOT) lga...
Quote:
I'm using the formula:
var
ship=round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20)*3)
+2)+((total7/20)*2)),2)
to calculate shipping charges, it works perfect. Now, I want to modify
this
formula so the minimum net result = 5 and the maximum = 30.

So if the calculated value equals less than 5, the result will be 5. If
exceeds 30, result will be 30.

I have tried different IF , <, > test permutations without success.

Any suggestions appreciated


if(ship<5)
ship = 5;
else if(ship>30)
ship = 30;

huh?

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <eternal.erectionN0 (AT) 5P4Mgmail (DOT) com>




Reply With Quote
  #3  
Old   
Edward
 
Posts: n/a

Default Re: IF & math formula student question - 08-30-2005 , 08:10 AM



Huh....
I must have syntax problems. My below test looks good, but doesn't work.

var ship_amt=
round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20)*3)+2)+(
(total7/20)*2)),2)
var ship
IF(ship_amt<5)
ship=5
else if(ship_amt>30)
ship=30
else ship=ship_amt



"Kimmo Laine" <eternal.erectionN0.5P (AT) Mgmail (DOT) com> wrote

Quote:
"Edward" <nomail (AT) yahoo (DOT) com> kirjoitti
viestissä:g2QQe.8923$Yh6.7258 (AT) fe04 (DOT) lga...
I'm using the formula:
var

ship=round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20)*3)
+2)+((total7/20)*2)),2)
to calculate shipping charges, it works perfect. Now, I want to modify
this
formula so the minimum net result = 5 and the maximum = 30.

So if the calculated value equals less than 5, the result will be 5. If
exceeds 30, result will be 30.

I have tried different IF , <, > test permutations without success.

Any suggestions appreciated



if(ship<5)
ship = 5;
else if(ship>30)
ship = 30;

huh?

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/
Kimmo Laine <eternal.erectionN0 (AT) 5P4Mgmail (DOT) com





Reply With Quote
  #4  
Old   
Edward
 
Posts: n/a

Default Re: IF & math formula student question - 08-30-2005 , 08:49 AM



I must have syntax problems. My below test looks good, but doesn't work.

var ship_amt=
round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20)*3)+2)+(
(total7/20)*2)),2)
var ship
IF(ship_amt<5)
ship=5
else if(ship_amt>30)
ship=30
else ship=ship_amt


"Kimmo Laine" <eternal.erectionN0.5P (AT) Mgmail (DOT) com> wrote

Quote:
"Edward" <nomail (AT) yahoo (DOT) com> kirjoitti
viestissä:g2QQe.8923$Yh6.7258 (AT) fe04 (DOT) lga...
I'm using the formula:
var

ship=round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20)*3)
+2)+((total7/20)*2)),2)
to calculate shipping charges, it works perfect. Now, I want to modify
this
formula so the minimum net result = 5 and the maximum = 30.

So if the calculated value equals less than 5, the result will be 5. If
exceeds 30, result will be 30.

I have tried different IF , <, > test permutations without success.

Any suggestions appreciated



if(ship<5)
ship = 5;
else if(ship>30)
ship = 30;

huh?

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/
Kimmo Laine <eternal.erectionN0 (AT) 5P4Mgmail (DOT) com





Reply With Quote
  #5  
Old   
Kimmo Laine
 
Posts: n/a

Default Re: IF & math formula student question - 08-30-2005 , 09:47 AM



"Edward" <nomail (AT) yahoo (DOT) com> kirjoitti
viestissä:%2_Qe.17075$ih4.15235 (AT) fe02 (DOT) lga...
Quote:
I must have syntax problems. My below test looks good, but doesn't work.

var ship_amt=
round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20)*3)+2)+(
(total7/20)*2)),2)
var ship
IF(ship_amt<5)
ship=5
else if(ship_amt>30)
ship=30
else ship=ship_amt

Naturally the IF should be in lowercase, ie. if, but I assume that's just a
typo. You might try debugging it with alerts. So let's modify the code a
bit.

var ship_amt=
round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20)*3)+2)+(
(total7/20)*2)),2)
alert( "1: " + ship_amt );
var ship
if( ship_amt < 5 ){
ship = 5;
alert("2: amount less than 5. " + ship);
} else if( ship_amt > 30 ) {
ship = 30;
alert( "3: amount more than 30. " + ship );
} else
ship = ship_amt;
alert( "4: amount between 5 and 30. " + ship );
}

See which alerts are triggered...

Out of curiosity: what does the round function do? Since it's not the
javascript native Math.round, does it come from your own function library? I
assume it has enhanced functionality in rounding to certain decimal, since
your giving it a second parameter which would seem like the precision to
me...

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <eternal.erectionN0 (AT) 5P4Mgmail (DOT) com>




Reply With Quote
  #6  
Old   
Edward
 
Posts: n/a

Default Re: IF & math formula student question - 08-30-2005 , 10:30 AM



Ok its working like this:

var
ship_amt=round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20
)*2)+2)+((total7/20)*2)),2)
var ship
if(ship_amt < 5)
ship = 5;
else if(ship_amt > 30)
ship = 30;
else
ship = ship_amt;

It didn't like the Alerts. Are the semi-colons important? I was using
lower case "if" in my tests.

function round(number,X) {return
Math.round(number*Math.pow(10,X))/Math.pow(10,X)}
Used with 2 assures two decimal place Cents, very important to PayPal
submission I hear(my next hurdle).

Thank you very very much.

"Kimmo Laine" <eternal.erectionN0.5P (AT) Mgmail (DOT) com> wrote

Quote:
"Edward" <nomail (AT) yahoo (DOT) com> kirjoitti
viestissä:%2_Qe.17075$ih4.15235 (AT) fe02 (DOT) lga...
I must have syntax problems. My below test looks good, but doesn't work.

var ship_amt=

round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20)*3)+2)+(
(total7/20)*2)),2)
var ship
IF(ship_amt<5)
ship=5
else if(ship_amt>30)
ship=30
else ship=ship_amt


Naturally the IF should be in lowercase, ie. if, but I assume that's just
a
typo. You might try debugging it with alerts. So let's modify the code a
bit.

var ship_amt=

round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20)*3)+2)+(
(total7/20)*2)),2)
alert( "1: " + ship_amt );
var ship
if( ship_amt < 5 ){
ship = 5;
alert("2: amount less than 5. " + ship);
} else if( ship_amt > 30 ) {
ship = 30;
alert( "3: amount more than 30. " + ship );
} else
ship = ship_amt;
alert( "4: amount between 5 and 30. " + ship );
}

See which alerts are triggered...

Out of curiosity: what does the round function do? Since it's not the
javascript native Math.round, does it come from your own function library?
I
assume it has enhanced functionality in rounding to certain decimal, since
your giving it a second parameter which would seem like the precision to
me...

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/
Kimmo Laine <eternal.erectionN0 (AT) 5P4Mgmail (DOT) com





Reply With Quote
  #7  
Old   
Kimmo Laine
 
Posts: n/a

Default Re: IF & math formula student question - 08-30-2005 , 12:04 PM



"Edward" <nomail (AT) yahoo (DOT) com> kirjoitti
viestissä:BY_Qe.17096$ih4.6418 (AT) fe02 (DOT) lga...
Quote:
Ok its working like this:

var
ship_amt=round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20
)*2)+2)+((total7/20)*2)),2)
var ship
if(ship_amt < 5)
ship = 5;
else if(ship_amt > 30)
ship = 30;
else
ship = ship_amt;
I don't know what you are doing differently, it looks just the same to me,
but good thing if it is finally working.

Quote:
It didn't like the Alerts.
Well, that's just too bad then. I really whish they could've been friends.

Quote:
Are the semi-colons important?
In my opinion: yes - when it comes to good programming practise and writing
readable code. It just clears things up when you definately see where a
statement ends.
In the scripts opinion: no - semicolons are optional. One line is one line
of code and that's that.

Quote:
I was using
lower case "if" in my tests.
That's what I thought.

Quote:
function round(number,X) {return
Math.round(number*Math.pow(10,X))/Math.pow(10,X)}
I expected just something like that.

Quote:
Used with 2 assures two decimal place Cents, very important to PayPal
submission I hear(my next hurdle).
You might get cought in this: round(4,2) returns '4', not '4.00' even if you
use your own round function. How to format even numbers to have the zeros
presented. that's a tricky one...

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <eternal.erectionN0 (AT) 5P4Mgmail (DOT) com>




Reply With Quote
  #8  
Old   
Jasen Betts
 
Posts: n/a

Default Re: IF & math formula student question - 10-15-2005 , 06:06 PM



On 2005-08-30, Edward <nomail (AT) yahoo (DOT) com> wrote:
Quote:
I'm using the formula:
var

ship=round((((total1/20)*2)+((total2/20)*2)+((total3/20)*2)+(((total6/20)*3)
+2)+((total7/20)*2)),2)
to calculate shipping charges, it works perfect. Now, I want to modify this
formula so the minimum net result = 5 and the maximum = 30.

So if the calculated value equals less than 5, the result will be 5. If
exceeds 30, result will be 30.


do the above working computation, then do this

ship=(ship<5)?5ship>30)?30:ship;

Quote:
I have tried different IF , <, > test permutations without success.

or you could use if

if(ship<5)ship=5;if(ship>30)ship=30;

Quote:
Any suggestions appreciated
javascript is case sensitive -- IF is not the same as if


Bye.
Jasen


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.