Forum Moderators: open

Message Too Old, No Replies

Setting up an external .js to return different quantity pricing

I think an array is necessary, need guidance more than solution!

         

whoisgregg

7:36 pm on Feb 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello! I am trying to make an external javascript that will return the price of a product based on the quantity entered into a form field. I want to be able to set the price breaks and the prices in each product page so I only need one .js file to handle all the variations.

Here's what I have now:


function getDiscount() {
var units = document.PriceCalc.Quantity.value;
if (units >= 200) {
var result = 2.99;
}
else if (units >= 100) {
var result = 3.99;
}
else if ((units <= 99) && (units >= 1)) {
var result = 4.99;
}
document.PriceCalc.ItemBasePrice.value = result;
}

Assuming that for any given product there is a variable number of quantity breaks, how would I:

a) store the variables in the HTML of the page? and

b) rewrite the function to accept the variables from the page?

I hypothesize that I could have two hidden text fields (PriceBreaks and BreakPrices) in each page with comma delimited variables and that I would use an array populated with those variables in the javascript function. Am I on the right track or is there a better way?

Thanks for any help you can provide! :)

Purple Martin

10:32 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hypothesize that I could have two hidden text fields (PriceBreaks and BreakPrices) in each page with comma delimited variables and that I would use an array populated with those variables in the javascript function. Am I on the right track or is there a better way?

You're on the right track using JavaScript arrays. The easy way to populate them would be to declare them directly in the JavaScript (no need for reading hidden fields). For example:

var PriceBreaks = new Array(0, 100, 200)
var BreakPrices = new Array(4.99, 3.99, 2.99)

You could do it with one multidimensional array, but using two parallel arrays like this works just as well and is straightforward to understand.

whoisgregg

3:44 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the reply... I am trying to define them in the body of the HTML so that I can have one external javascript do all the processing for all the different product pages. I'm glad I'm right about using arrays!

whoisgregg

8:58 pm on Feb 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, here we go... I can define an array with the contains of a text field like so:

quanLoad = new String(document.PriceCalc.quanArr.value);
var quanArray = quanLoad.split("/");

However, this makes each element of the array be a string and not a number. How do I change each member of a string into a number?

Purple Martin

9:54 pm on Feb 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The parseInt() function can be used to convert a string variable to a numeric variable.

After you've populated the array, make a simple 'for' loop to go through all the items in the array and convert the type of each.

Alternatively, leave the array items as strings, and use parseInt() at the point in the code that you use each value.

whoisgregg

10:03 pm on Feb 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Like so:

quanArray = new String(document.PriceCalc.quanArr.value);
quanArray = quanArray.split("/");
for (i=0; i < quanArray.length; i++) {
quanArray[i] = parseInt(quanArray[i]);
}

Perfect! thanks purple martin!

whoisgregg

10:27 pm on Feb 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



so where's the best place for me to post my finished solution for anyone else to use? I searched high and low for a price break javascript and couldn't find one, now that I've written one, how should I distribute it for everyone's benefit?

Thanks to everyone for their help! Credit will be given in the javascript comments.

Purple Martin

12:45 am on Feb 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Personally I like to post my code on Planet Source Code, and there are many other equally good sites. Once you've posted on one site, it doesn't take long for it to find it's way into many other sites and into Google.

whoisgregg

7:53 pm on Feb 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's been posted there by the name of PriceBreakCalculator under the category of "java/javascript:External .JS files" Thanks!