Small Java Script Question...

They have: 35 posts

Joined: Aug 2001

I have the following HTML for a page I am working on but it doesn't seem to work out.

Discount Test
<script language="JavaScript">
function do_discount()
{

var total_ordered=document.get_data.number_ordered.value;
var total_cost=document.get_data.number_ordered.value*document.get_data.cost.value

if(total_ordered>=0 && total_ordered >24)
{
total_cost=total_cost-(5000*document.get_data.number_ordered.value);
}
if(total_ordered>=24 && total_ordered >48)
{
total_cost=total_cost-(1000*document.get_data.number_ordered.value);
}
else if(total_ordered>=48 && total_ordered >72)
{
total_cost=total_cost-(15000*document.get_data.number_ordered.value);
}
document.get_data.answer.value=total_cost;
}
</script>

Number:

Row ABC

What I want it do is this:

IF Quantity is 24 > THEN Discount is 5000 per Ship
IF Quantity is 48 > THEN Discount is 10000 per Ship
IF Quantity is 72 > THEN Discount is 15000 per Ship

But as you'll notice if you try it the figures don't come out right.

dk01's picture

He has: 516 posts

Joined: Mar 2002

Ok this code could be made cleaner but basically you had three problems.

1) All values that you get using the document object will return a string (a group of letter/numbers). Try doing the following two groups of code and see what happens:

alert("2400" + "2400");

and

alert(2400 + 2400);

With the first one javascript treats the values at strings (or floats) and in the second one it treats them as numbers (or integers).
For this problem we can use the parseFloat() function to convert a string to an integer.
Therefore:

alert( parseFloat("2400" + "2400") );
and
alert(2400 + 2400);

will return the same result.

2) Your second problem was the greater than or less than symbols that you were using in your if statements. You had:

if(total_ordered>=0 && total_ordered >24)

This says in literal terms 'If total_ordered is greater than or equal to 0 and total_ordered is greater than 24'. You just needed to change the second symbol to a less than symbol:
if(total_ordered>=0 && total_ordered <24)

3) The last was just a typo where you said:
total_cost=total_cost-(1000*total_ordered);

I think you meant to have 10000 instead like:
total_cost=total_cost-(10000*total_ordered);

So here would be your updated code:

function do_discount()
{

var total_ordered = parseFloat(document.get_data.number_ordered.value);
var cost = parseFloat(document.get_data.cost.value);
var total_cost = total_ordered * cost;

if(total_ordered>=0 && total_ordered <24)
{
total_cost=total_cost-(5000*total_ordered);
}
else if(total_ordered>=24 && total_ordered <48)
{
total_cost=total_cost-(10000*total_ordered);
}
else if(total_ordered>=48 && total_ordered <72)
{
total_cost=total_cost-(15000*total_ordered);
}
document.get_data.answer.value=total_cost;
}
'
Hope I helped you understand. Good luck learning Javascript! You'll learn to love it eventually. Smiling
-dk

They have: 35 posts

Joined: Aug 2001

Okay 've had a fiddle around with the code and 've got it right apart from one small thing. When I enter a quantity of 47 it multiplies it by 545000 (answer = 25615000) when it should multiply it by 540000 (answer = 25380000). The code is below, any idea why it does that?

function do_discount()
{

var total_ordered = parseFloat(document.get_data.number_ordered.value);
var cost = parseFloat(document.get_data.cost.value);
var total_cost = total_ordered * cost;

if(total_ordered>=0 && total_ordered <23)
{
total_cost=total_cost-(0*total_ordered);
}
else if(total_ordered>=24 && total_ordered <47)
{
total_cost=total_cost-(5000*total_ordered);
}
else if(total_ordered>=48 && total_ordered <71)
{
total_cost=total_cost-(10000*total_ordered);
}
else if(total_ordered>=72 && total_ordered <200)
{
total_cost=total_cost-(15000*total_ordered);
}
document.get_data.answer.value=total_cost;
}

dk01's picture

He has: 516 posts

Joined: Mar 2002

Ok I am not sure which of the promblems you are experiencing but depending on what you are trying to do it could be either of them.

Either:

You possibly typed in the wrong cost. If you look at your cost feild in the html you will see that its value is set to 545000.

or:

You are possibly looking at the code wrong. Lets look at this section:

else if(total_ordered>=24 && total_ordered <47)
{
total_cost=total_cost-(5000*total_ordered);
}
'This code says 'if the order quantity is between 24 and 47 then multiply the quantity by 5000 and subtract it from the original cost of the order [ total_cost = 25615000-(5000*47) ].
The reason I wrote that was because in you original post you said you wanted to subtract 5000 per ship that was purchased. Is this what you meant or did you mean that it would just be a 5000 discount on the total price?
-dk

They have: 35 posts

Joined: Aug 2001

I meant per ship.

http://www.centrepointstation.net/test/test.htm

Type in 47 and you'll see that you get 2561500 which is 47*545000 not 47*540000

dk01's picture

He has: 516 posts

Joined: Mar 2002

Yes I can see that. But according to your code that is the correct answer because you have the cost (The value of the hidden feild cost) actually set to 545000. If you go to your html code and find the hidden feild called 'cost' you will see its value is 545000. If you change this yo 540000 then you should get the right answer.
-dk

They have: 35 posts

Joined: Aug 2001

But I want the base cost to be 545000.

< 23 Price = 545000
24 < 47 Price = 540000
48 < 71 Price = 535000
72 > Price = 530000

See?

dk01's picture

He has: 516 posts

Joined: Mar 2002

ok then you should do this:
function do_discount()
{

var total_ordered = parseFloat(document.get_data.number_ordered.value);
var cost = parseFloat(document.get_data.cost.value);
var total_cost = total_ordered * cost;

if(total_ordered>=0 && total_ordered <23)
{
total_cost=total_cost-0;
}
else if(total_ordered>=24 && total_ordered <47)
{
total_cost=total_cost-5000;
}
else if(total_ordered>=48 && total_ordered <71)
{
total_cost=total_cost-10000);
}
else if(total_ordered>=72 && total_ordered <200)
{
total_cost=total_cost-15000;
}
document.get_data.answer.value=total_cost;
}

-dk

They have: 6 posts

Joined: Apr 2002

hmm it seems to me that the new code you posted does:

Quantity * Base Price - X

instead of

Quantity * Base Price - X * Quantity

dk01's picture

He has: 516 posts

Joined: Mar 2002

Ugh this is getting frustrating:

Try this:

function do_discount()
{
var total_ordered = parseFloat(document.get_data.number_ordered.value);
alert(total_ordered);
if(total_ordered >= 0 && total_ordered < 24)
{
unitprice = 545000;
}
else if(total_ordered >= 24 && total_ordered < 48)
{
unitprice = 540000;
}
else if(total_ordered >= 48 && total_ordered < 72)
{
unitprice = 535000;
}
else if(total_ordered >= 72 && total_ordered < 200)
{
unitprice = 530000;
}
total_price = unitprice * total_ordered;
document.get_data.answer.value=total_price;
}
'
-dk

dk01's picture

He has: 516 posts

Joined: Mar 2002

Hope that worked. I am away until this Saturday night. Just don't want you to think I'm ignoring you Sticking out tongue
-dk

They have: 35 posts

Joined: Aug 2001

Well it worked!! Smiling

http://www.centrepointstation.net/css/index3.htm

Services > Ship Prices

Figured you'd helped so much you deserved to be shown the final product. Thanks for all yer help mate.

dk01's picture

He has: 516 posts

Joined: Mar 2002

Great! No probs. Looks great in action!
-dk

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.