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

# Thread: Special price calculator

1. ## Special price calculator

Hallo.
I am a total newbie to JavaScript coding, but I would like to have a price calculator on our internal website.

It should calculate the square meter of the product and then multiply it with the square meter price, multiply it with quantity and then plus it with the startup cost. But... if the square meter extend 20 m2 the square meter price should be lower, and also lower when it hit 50 m2. And at 100 m2 it should write that you have to contact sales department.

Can this be done with JavaScript?

Here is an example:
6 pieces of 2 x 4 meter.

2x4 = 8 m2
6x8 = 48 total m2
48 m2 x price2 x startup = total price

I really hope that someone can help me...

//Daniel

• It can indeed be done with Javascript - but the forum is not a free coding service, especially for commercial applications, and in any case your examples are too vague and incomplete to be useful. My advice would be to learn JavaScript (and HTML), then if you have specific coding problems feel free to come back for guidance/hints/corrections.

I expect you realise that the biggest part of any code such as this is validation - that the values entered by the user are sensible, so if for example a number is expected a non-numeric value is rejected.

It is your responsibility to die() if necessary….. - PHP Manual

• Hi Philip.

Thank you for you answer and I fully understand you.

I all ready have a JavaScript calculator, but I would like to extend it.

I would like to have more "basisprice".
So I one price for 1 - 20 (quantity x high x wide = m2)
One price for 20 - 50
And one price for 50 - 99

Can this be done?

Check this script:

<script type="text/javascript">
function beregn() {
var basisprice = 70;

var high = document.calcutalor.high.value;

if (isNaN (high) || high < 0 || high >10) {
alert ("Number must be between 1 - 10");
document.calcutalor.high.value = "";
document.calcutalor.high.focus();
return false;
}

var wide = document.calcutalor.wide.value;

if (isNaN (wide) || wide < 0 || wide >10) {
alert ("Number must be between 1 - 10");
document.calcutalor.wide.value = "";
document.calcutalor.wide.focus();
return false;
}

var quantity = document.calcutalor.quantity.value;

if (isNaN (quantity) || quantity < 0 || quantity >10) {
alert ("Number must be between 1 - 10");
document.calcutalor.quantity.value = "";
document.calcutalor.quantity.focus();
return false;
}

var total = "Total price: ";

totalprice = (quantity * high * wide * basisprice) + 50;
document.getElementById('result').innerHTML = totalprice;
}
</script>

• That's more like it!

Code:
```var sqmtrs = high*wide;
var basisprice1 = 70;  // or whatever the price is
var basisprice2 = 60;
var basisprice3 =  55;

if (sqmtrs > 99) {alert ("Contact Sales Department"); return false}
if (sqmtrs<=20) {totalprice = (quantity * sqmtrs * basisprice1) + 50};
if (sqmtrs>20 && sqmtrs <=50) {totalprice = (quantity * sqmtrs * basisprice2) + 50};
if (sqmtrs>50) {totalprice = (quantity * sqmtrs * basisprice3) + 50};
document.getElementById('result').innerHTML = totalprice.toFixed(2);  // 2 decimal places```
Note you cannot have a price for 1-20 and also for 20-50 (must be 21 to 50).
I am unclear if the sizes must be integers or whether decimal values are allowed. You might prefer

var sqmtrs = Math.ceil(high*wide); // round up to nearest integer

To overcome a bug in Firefox, replace
document.formname.fieldname.focus();
by
setTimeout("document.formname.fieldname.focus()", 25);

• Hi Philip.

Thank you very much for your answer. I appreciate it.
But for some reason I cant get it to work. When I make a calculation it is all ways using basisprice1, no mather what I enter.

Do you know what I have done wrong??

Here is the full script:

<script type="text/javascript">
function calculate() {
var basisprice1 = 100;
var basisprice2 = 90;
var basisprice3 = 80;

var high = document.calculator.high.value;

if (isNaN (high) || high < 0 || high >50) {
alert ("Enter 1-50");
document.calculator.high.value = "";
document.calculator.high.focus();
return false;
}

var width = document.calculator.width.value;

if (isNaN (width) || width < 0 || width >50) {
alert ("Enter 1-50");
document.calculator.width.value = "";
document.calculator.width.focus();
return false;
}

var quantity = document.calculator.quantity.value;

if (isNaN (quantity) || quantity < 0 || quantity >200) {
alert ("Enter 1-200");
document.calculator.quantity.value = "";
document.calculator.quantity.focus();
return false;
}

var total = "Totalprice: ";
var piece = "Price per piece: ";
var kr = " DKK.";

var sqmtrs = high*width;
if (sqmtrs > 99) {alert ("Contact Sales Department"); return false};
if (sqmtrs<=20) {totalprice = (quantity * sqmtrs * basisprice1) + 50};
if (sqmtrs>20 && sqmtrs <=50) {totalprice = (quantity * sqmtrs * basisprice2) + 50};
if (sqmtrs>50) {totalprice = (quantity * sqmtrs * basisprice3) + 50};
document.getElementById('piece').innerHTML = piece;
document.getElementById('piece-pris').innerHTML = totalprice / quantity;
document.getElementById('result').innerHTML = totalprice.toFixed(2);
document.getElementById('total').innerHTML = total;
document.getElementById('kr').innerHTML = kr;
document.getElementById('kr2').innerHTML = kr;
}
</script>

• Um...Philip, isn't this is *EXACTLY* the same homework problem you gave the same answer for a few months back??

poulsen: The code as given in your last post worked 100% correctly for me.

Except: The discount pricing is based *ONLY* on the height and width. The quantity is not used in calculating the discount. That's now what you asked for in your original post.

• Off course...
Now it works, thank you...

Do you know how I can return the price pr. piece with two decimals?
document.getElementById('piece-pris').innerHTML = totalprice / quantity;

• I found the solution.

Thank you very much for your help

•

#### LinkBacks (?)

1. ###### JavaScript - How To Code For A Price Quote Calculator? - AllWebDevHelp.com
Refback This thread
06-13-2014, 01:51 PM
2. ###### JavaScript - Special Price Calculator - AllWebDevHelp.com
Refback This thread
04-18-2014, 04:22 PM

#### Posting Permissions

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