...

View Full Version : calculation problem that's made me bald!



grahamtinley
07-19-2012, 01:20 PM
Here's the script part:

var price0 = (rate * length).toFixed(0);
var cut = Number(document.form.cut.value);

if (cut >=1 <=3) {
var cuts = cut*100;}
if (cut >=4 <=10) {
var cuts = cut*80;}
if (cut >10) {
var cuts = cut*60;}

document.form.price1.value = price0 + cuts;

If I remove + cuts the price shows accurately.

With + cuts added, the price1 = price0 multiplied by 100 plus 100.

Any help greatly appreciated.

Philip M
07-19-2012, 02:30 PM
toFixed() converts the value to a string. So document.form.price1.value = price0 + cuts; concatenates the two values, not adds them.

You need to change your code to

var price0 = parseInt((rate * length),10);
and then
document.form.price1.value = (price0 + cuts).toFixed(0);

but if your display is integer numbers there is not need to convert price0 to an integer using parseInt();

if (cut >=1 <=3) {
should be if ((cut>=1) || (cut <=3)) {

var cut = Number(document.form.cut.value); - avoid using the same name for an HTML element and a Javascript variable.


"Dives sum, si non redo eis quibus debeo. - I am a rich man as long as I do not pay my creditors"

grahamtinley
07-19-2012, 02:50 PM
hmmm... I must have got something wrong...

var cuts = Number(document.form.cut.value);

if ((cuts>=1) || (cuts <=3)) {
var cutstotal = (cuts*100);
}
if ((cuts >=4) || (cuts<=10)) {
var cutstotal = (cuts*80);
}
if (cuts >10) {
var cutstotal = (cuts*60);
}

var price0 = parseInt((rate * length),10);
document.form.price1.value = (price0 + cutstotal).toFixed(0);

*************
with two cuts, the price1 value should be price0 + 2 X 100, it comes out at 159.

Philip M
07-19-2012, 03:05 PM
Error - your || or should be && and.


if ((cuts>=1) && (cuts <=3)) { // more than or equal to 1, less than or equal to 3
var cutstotal = (cuts*100);
alert (cutstotal)
}
if ((cuts >=4) && (cuts<=10)) { // more than or equal to 4, less than or equal to 10
var cutstotal = (cuts*80);
}


Make sure that cuts cannot be a decimal number say 3.5. Use parseInt().

grahamtinley
07-19-2012, 03:49 PM
Awesome :D:D:D:D:D:D:D

Even saw a hair growing...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum