# JavaScriptJavascript form calculation?

##### Coder
Hi Guys,

I need help with this calculation, im designing a form for a client that needs a MAX PRICE between two values. I'm using the javascript below but this makes each quantity 250, I need it so its a maximum of 250 Upto 24999 and then ill do another value between 25000 up to 29999 and the price would be 275.

JavaScript:
``````if([item-417_quantity]<24999){
[price]=250
}``````

I'll appreciate anyone's help, Thanks guys!!

#### Johna

##### Frontend Web Developer
Staff Team
Guardian
I need it so its a maximum of 250 Upto 24999 and then ill do another value between 25000 up to 29999 and the price would be 275.
I don't really understand this sentence, could you please explain a bit more?

##### Coder
I don't really understand this sentence, could you please explain a bit more?
Hi Johna,

Sorry. The price of the service is \$250. upto a value of <24,999, then increases to \$275 from between >25,000 upto <29,999. I need this calculation in java, currently the calculation i've used, stops at 24,999 like i want but each 1 equals \$250.

Hope this makes more sense

##### Coder
I don't really understand this sentence, could you please explain a bit more?
As you can see in the screenshot, my current code is making each 1 quantity £250 Upto 24,999 in that top slider but needs to be \$250 total, currently in the screenshot, its working out at 10,000 X \$250

#### Attachments

• Screenshot (349).png
139.9 KB · Views: 3

#### Johna

##### Frontend Web Developer
Staff Team
Guardian
Something like this?
JavaScript:
``````if (quantity < 25000) {
var price = 250 * quantity;
} else if (quantity < 30000) {
var price = 275 * quantity;
}``````

##### Coder
Something like this?
JavaScript:
``````if (quantity < 25000) {
var price = 250 * quantity;
} else if (quantity < 30000) {
var price = 275 * quantity;
}``````
Yes, something like that but unfortunately it didn't work, I even tried this code as well, as I thought it may be to do with the form values but still not luck.

JavaScript:
``````if (item-417_quantity < 25000) {
[price] =([item-417_price])= 250;
} else if (item-417_quantity < 30000) {
[price] =([item-417_price])= 275;
}``````

#### Attachments

• Screenshot (354).png
29.4 KB · Views: 2

#### Johna

##### Frontend Web Developer
Staff Team
Guardian
JavaScript variable names can't contain a `-`. Try `item_417_quantity` instead of `item-417_quantity`

##### Coder
JavaScript variable names can't contain a `-`. Try `item_417_quantity` instead of `item-417_quantity`
The Item Number doesn't turn green and didn't work.

#### Johna

##### Frontend Web Developer
Staff Team
Guardian
item-417_quantity
is this one variable or is it a variable `417_quantity` subtracted from a variable `item`?

##### Coder
JavaScript:
``item-417``
is referring to the form item/field.

JavaScript:
``_quantity``
Should be the amount stated within the field.

If
JavaScript:
``item-417``
Quantity is <25000, then total price is 250

#### Johna

##### Frontend Web Developer
Staff Team
Guardian
so are you trying to connect two strings together? I don't get why the two variables are connected?

##### Coder
Apologies for being a pain I've attached another screenshot, of a working calculation for multiplying two fields together, I hope this will help with understanding the structure

#### Attachments

• Screenshot (361).png
29 KB · Views: 2

#### Johna

##### Frontend Web Developer
Staff Team
Guardian
JavaScript:
``([item-3_price]/2) * [item-1_quantity]``
This part is being multiplied, not `[item-3_quantity]`

From what I understand `[item-3_quantity]` is a variable?

##### Coder
JavaScript:
``([item-3_price]/2) * [item-1_quantity]``
This part is being multiplied, not `[item-3_quantity]`

From what I understand `[item-3_quantity]` is a variable?
Yes

#### Johna

##### Frontend Web Developer
Staff Team
Guardian
Ok, so re-reading your question, I think this is what you mean, correct me if I'm wrong.
• You have an item that you're selling, let's say they're teddy bears.
• If you buy 24,999 or less, each teddy bear costs \$250, and the total price costs 250 multiplied by the number of teddy bears.
• If you buy 25,000 up to 30,000, each teddy bear costs \$275, and the total price costs 275 multiplied by the number of teddy bears.
• You can't buy more than 30,000 teddy bears.
• Now you're writing a function to calculate the total price of all the teddy bears the customer is buying
• In your function, you have a variable `[item-417_quantity]` which is the number of teddy bears
• You also have a variable `[price]`, which is the price of each teddy bear

First rename your variables, because variable names can only contain letters, numbers, underscores and dollar signs. You have 3 unallowed characters in you variable: `[`, `-` and `]`.

JavaScript:
``````if (quantity < 25000) {         //
var price = 250 * quantity;   //
} else if (quantity < 30000) {  //  replace quantity and price with your variables
var price = 275 * quantity;   //
}                               //``````
If this still doesn't work, then could you post some more of your code? Are you getting any errors in the console?

Last edited:

##### Coder
JavaScript:
``([item-3_price]/2) * [item-1_quantity]``
This part is being multiplied, not `[item-3_quantity]`

From what I understand `[item-3_quantity]` is a variable?

##### Coder
Hi Johna, First can I say thank you so much for being so much help, it really is appreciated, your a diamond 😊

Close- if the teddy bears value is between 0 to 25,000 the price of the teddy bear is \$250 total (maximum).

The below code works for the first two in between values but each \$1 of teddy bear value, the price is \$250, I need it to not go above \$250 total price between the values.

JavaScript:
``````if ([item-417_quantity] < 25000) {
[price] = 250
}``````

#### Johna

##### Frontend Web Developer
Staff Team
Guardian
Hi Johna, First can I say thank you so much for being so much help, it really is appreciated, your a diamond 😊

Close- if the teddy bears value is between 0 to 25,000 the price of the teddy bear is \$250 total (maximum).

The below code works for the first two in between values but each \$1 of teddy bear value, the price is \$250, I need it to not go above \$250 total price between the values.

JavaScript:
``````if ([item-417_quantity] < 25000) {
[price] = 250
}``````
So if the total is \$250, does that mean each is \$0.01?

##### Coder
It's a car insurance form, so the slider i'm currently trying these calculations on is for the user to give a value of their car and the cost for insurance is depending on the value of the car. so cars upto 24,999 in value will cost \$250. hope this makes more sense.

#### Johna

##### Frontend Web Developer
Staff Team
Guardian
If it's like that, they why doesn't this work, have you checked the console for any errors?
JavaScript:
``````if (quantity < 25000) {         //
var price = 250 * quantity;   //
} else if (quantity < 30000) {  //  replace quantity and price with your variables
var price = 275 * quantity;   //
}                               //``````

Replies
1
Views
425
Replies
0
Views
328
Replies
3
Views
464
Replies
0
Views
470