/    Sign up×
Community /Pin to ProfileBookmark

Weight Calculation

Can someone please help me with this calcuation? A person enters their height, length, width and weight of their parcel. The calculate button then runs the function below to calculate the volumetric weight. The total price is based on which of the two weights (normal weight entered by the user and volumetric weight) is the highest. The total price is also based on which radio box is selected, hence the “svc” code below for “Silver Service” which is $599 and “Gold Service” which is $699.

Currently this script works fine but as soon as I enter a weight of less than 10, it runs the calculation on that number instead of the higher one (volumetric) as it should. I’m sure there’s an easier way to grab the higher number of the two to run the calculation.

Please, can anybody help a total newbie??

Here’s my code:

[CODE]function compute_vol() {
var weight_calc =
(document.form.height.value*1)*
(document.form.length.value*1)*
(document.form.width.value*1)/6000;

document.form.weight_vol.value = weight_calc.toFixed(0);

if ((document.form.length.value == ”) || (document.form.width.value == ”) || (document.form.height.value == ”) || (document.form.weight.value == ”)){
alert(“In order to proceed with your order, please ensure that you’ve entered a value into all the dimension fields then click on ‘Calculate Volumetric Weight’ again.”);
document.form.Submit2.disabled = true;
return false;
}

svc = “”;
for (var i=0; i<document.form.selection.length; i++)
{if (document.form.selection[i].checked)
{svc = document.form.selection[i].value;}
}

if ((svc == “Silver Service”) && ((document.form.weight.value) > (document.form.weight_vol.value))) {
document.form.totalprice.value = (((document.form.weight.value) – 10) * 50 ) + 599;

} else if ((svc == “Silver Service”) && ((document.form.weight_vol.value) > (document.form.weight.value))) {
document.form.totalprice.value = (((document.form.weight_vol.value) – 10) * 50 ) + 599;
}

if ((svc == “Gold Service”) && ((document.form.weight.value) > (document.form.weight_vol.value))) {
document.form.totalprice.value = (((document.form.weight.value) – 10) * 50 ) + 699;
} else if ((svc == “Gold Service”) && ((document.form.weight_vol.value) > (document.form.weight.value))) {
document.form.totalprice.value = (((document.form.weight_vol.value) – 10) * 50 ) + 699;
}[/CODE]

Thanks!!

Karen

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@KorJun 26.2008 — You forgot that the [B]value[/B] attribute (as all the HTML attributes) returns a [I]string[/I], not a decimal number. The comparison (and sorting) of two strings is made alphabetically, character by character, not mathematically. Have a look:
<i>
</i>alert('10'&gt;'9');//false, because 1 is before 9 in the alphabetical list
alert(10&gt;9);//true

Before a math comparison or a math subtraction the strings must be transformed into decimal numbers:
<i>
</i>if (svc == "Silver Service" &amp;&amp; [COLOR="Blue"]Number([/COLOR]document.form.weight.value[COLOR="Blue"])[/COLOR] &gt; [COLOR="Blue"]Number([/COLOR]document.form.weight_vol.value[COLOR="Blue"])[/COLOR])
Copy linkTweet thisAlerts:
@shakeukJun 26.2008 — @ Kor

your using [COLOR="Blue"]Number(document.form.weight.value)[/COLOR]

would this work?

[COLOR="blue"]parseInt(document.form.weight.value)[/COLOR]

i belive it will and have never used Number() before which one is best?

i know parseInt() removes any letters from a string which contains a numerical value.

could you explain for me (cos im learning) why use one over the other and what situation you "normally" use them in?

thanks.
Copy linkTweet thisAlerts:
@JS_FreakauthorJun 26.2008 — Kor, you are a star! It works 100% now. Thanks for pointing out the Number bit to me.


Karen
Copy linkTweet thisAlerts:
@KorJun 26.2008 — There are several ways to transform a string into a decimal number (or to parse

the integer/floated), amongst the most used are:

[B]

- Number([I]string[/I]) [/B]


I prefer this method. [I]It will transform also a empty value into 0, which is very useful in form's calculation[/I].

[B]- parseInt([I]string,radix[/I])[/B]

A complex method. It will translate a number from a certain base (radix from 2 up to 36) to the decimal base as an integer. It is used also to extract the integer part of mixed strings (value+measurement unit) or floated values.

Ex:

parseInt('ff',16) //returns 255

parseInt('27px') //returns 27

parseInt('27.56') //returns 27

[B]- parseFloat([I]string[/I])[/B]

Simply transforms a string into a floated decimal (of course, if the stringed value is a floated)

Ex:

parseFloat('27.56') //returns 27.56

[B]- multiply or division with/by 1. [I]string[/I]*1 | [I]string[/I]/1[/B]

'27.56'*
1 //returns 27.56

'27.56'/1 //returns 27.56
Copy linkTweet thisAlerts:
@toicontienJun 26.2008 — I wrote a blog post about numbers, and converting strings to number using JavaScript: A Practical Guide To Numbers in JavaScript. It covers all the methods Kor covered above, including the drawbacks and advantages of converting strings to numbers in various ways.
×

Success!

Help @JS_Freak spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...