Expand my Community achievements bar.

using an array with a function

Avatar

Level 2

Hi all,

I'm working on an order form where users choose a course they want to order from a drop-down list, then enter the number of licences they require. I'm trying to build in a 'volume-based' discount model the form, and would like to ask for some help in using arrays and how to use them in functions. I'm very new to this, and would appreciate any help you can provide!

Basically, we have 3 products that can be ordered on the same form, and all have different discount models (i.e you get a different % discount for different numbers of licences ordered, depending on the course chosen). I'm storing the discount models in 3 separate two-dimensional arrays, then want to create a function that takes two parameters - the name of the array and the volume that the customer orders - to calculate the appropriate discount. I would then call the function on the change events of either the drop down list or the licence qty field.

Below is my attempt so far (only one array here, but the others are the same syntax). This is held in a script object called DiscountCalculator in the form. The idea of the function is to loop through the array until it finds the correct course, then loop through the value field until it finds the correct discount:

//code holds the discount model for each course in a 2 dimensional array. A new array is needed for each course.

//Course 1

var

Course1Array = new Array(3);

Course1Array[0]

= new Array(2);

Course1Array[0][0]

= "49";  //'breakpoint' at which discount is applied

Course1Array[0][1]

= "0";   //discount % given for this no of licences

Course1Array[1]

= new Array(2);

Course1Array[1][0]

= "99";

Course1Array[1][1]

= "20";

Course1Array[2]

= new Array(2);

Course1Array[2][0]

= "199";

Course1Array[2][1]

= "25";

function

CalculateDiscount(CourseArray, value){

for

(var i = 0; i <= CourseArray.length; i++)

{

if

(value <= CourseArray[i][0])

{

return CourseArray[i][1]

}

}

}

I can use a MessageBox to return a specified value from the array on the click event, so that seems to be set up correctly. But when I try to tcall the function in the change event of the drop-down list on the form:

DiscountCalculator.CalculateDiscount(fldCourse.rawvalue, fldqty.rawvalue)

It doesn't work, usually complaining that "Course1Array is not defined". I think it must be some kind of syntax error, but I have trawled the internet looking for some examples and can't find any.

I'd be grateful for any assistance.

Thanks

Matt

4 Replies

Avatar

Level 10

It might be easy to solve these issues if you post your form.

Post your form if it doesn't contain any confidential information.

Nith

Avatar

Level 2

Unfortunately the forums pages don't seem to be loading - is there somewhere that I can email the form to?

Thanks

Matt

Avatar

Level 2

I don't know if this will work, but I'm attaching the form to this email in the hope that it will get posted to the forum. As I mentioned early, the forum pages simply will not load at the moment.

Obviously this is a testing form - once I understand the concept and how to structure the code I will look to implement it more thoroughly on the real form.

Thanks for your help.

Matt

Avatar

Level 2

Just as a follow up - following some great help from Kevin who emailed me, this has now been resolved. The array was re-designed and some syntax errors in the function addressed. If anyone is interested to see the final code just send me a message on here and I'll provide it.

Many thanks to Kevin for all his help!

Matt