/    Sign up×
Community /Pin to ProfileBookmark

Search an array with variables to find a value

Hi all. I’ve got what is likely a fairly simple problem, but I haven’t been able top find a good example and I am new to JS.

I am implementing a shopping cart program and the shipping calculation is not robust enough to support our needs. What I want to do is create a custom function to calculate the shipping cost based on three varialbes and then pass the result on to the shopping cart. The shopping cart shipping calculation would be turned off.

So, when the customer wants to buy a product, they must select values from three separate drop downs: Number of products to order, Shipping zone, and Type of service. For example, 2 widgets, being shipped to zone 4, using ground service. Based on the values selected in the drop downs, an array/table should be searched to match on those three values and retrieve a fourth value, which would be the shipping cost for that combination. The resulting shipping cost will then be passed on to the cart program.

I would greatly appreciate any help or insight that anyone could provide. Well documented examples are especially appreciated!

Thanks everyone!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 06.2006 — Well documented examples are especially appreciated![/QUOTE]
Don't want much for free, eh? ?

Have you worked with 2-dimensional arrays?
Copy linkTweet thisAlerts:
@RobHauthorJun 06.2006 — Don't want much for free, eh? ?

Have you worked with 2-dimensional arrays?[/QUOTE]


I'll obviously take any help that I can get, but it would really help me learn if I had some hints to let me know what is going on.

I have pretty much zero JS experience, other than reusing published functions. However, I am an old COBOL programmer and understand programming and array logic.

I was thinking that for this case, I'd have my shipping lookup table with 4 columns - # of product, zone, ship method, and cost. I anticipate about 150-200 entries in this table. Is it possible to have this be an external text file?

I would then anticipate setting up some kind of loop to compare the user selected qty, zone and method variables with each row in the table. So if a=a and b=b and c=c, then get the cost and return it back. If no match, then move on to the next row. So it's really more of a 1-D array than a 2-D array.

So that's what I guess the main function logic would be. But then I'm also shaky on how to pass the form variables to the function and pass the result back to the form.
Copy linkTweet thisAlerts:
@phpnoviceJun 06.2006 — I've been programming in COBOL (plus over a dozen other programming languages) for about 26 years now. But, it is definitely a 2-dimensional array. Tables have two dimensions -- rows and columns. Yes, it can be in an external file. However, keep in mind that JavaScript can be bypassed and, as such, perhaps you'll also need this table on the server-side in order to check it again.

The following is what would go in your external JavaScript file:
var qty = 0, zone = 1, method = 2, cost = 3;
var shipping = new Array(
new Array( 1, 0, 'GROUND', 2.00),
...etc...
new Array(999, 999, 'NEXT DAY AIR', 99.99)
); // no comma after last entry above
×

Success!

Help @RobH spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...