Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## calculation problem that's made me bald!

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.

• 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"

• 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.

• Error - your || or should be && and.

Code:
```if ((cuts>=1) && (cuts <=3)) {  // more than or equal to 1, less than or equal to 3
var cutstotal = (cuts*100);
}
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().

• Awesome

Even saw a hair growing...

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•