tonyeck
12-04-2004, 11:52 PM
Hey all :)
I have a small sorting problem within Javascript and 2D arrays. Basically i have created an array like so inside a function which calculates the final out put:
function go(){
var table1= [
["Kestrel Moon", 1165, 475, 0, 97, 0, 98],
["BB", 1231, 380, 89, 43, 180, 85],
["Whatever Your Drive", 1052, 210, 34, 23, 450, 43],
["Discount Drivers", 1667, 310, 291, 302, 189, 93],
["Indirect Curve", 804, 599, 0, 45, 0, 0],
["Swindel", 1010, 210, 187, 35, 32, 103],
["Captain", 510, 100, 0, 19, 0, 40],
["Saintly Drivers", 890, 0, 0, 0, 0, 155],
["Ipswich Union", 490, 0, 0, 0, 0, 0],
["Young Drivers", 803, 0, 203, 78, 0, 82],
["Atlee", 923, 200, 107, 81, 92, 70],
["Good Deal", 1690, 300, 434, 112, 493, 47],
["Nice Price", 775, 0, 0, 487, 0, 30],
["Insure for Less", 1435, 0, 998, 49, 0, 59],
["Instant Insure", 1223, 424, 43, 113, 68, 96]
];
The whole point of this for me is to learn javascript functions, calculations and sorting these. So the arrays basically are values for discounts from the insurance company (i.e. kestrel moon) and you are given discount say if you were above a certain age:
for (var i=0;i<table1.length;i++)
{
//______________________Defining the total cost
total_cost = table1[i][1];
//______________________If statments
//______________________If age is less than 25 - discount!
if (age > 25)
{
total_cost = total_cost - table1[i][2];
parent.frames.bottom.document.write("Over 25 years old: £"+table1[i][2]+"<BR>")
}
I have a whole bunch of If statments working out the discounts etc. Then from a form (where the user inputs data like age etc) a function is run (where all the if statements etc are kept). The results are then writen to a new frame. I want, from the output, to be able to show the user what the best price would be. The "total_cost" is worked out after all the discounts have been subtracted and was wondering how to pick out the lowest result?
This is the code which writes (and loops to write for each company) the final cost:
parent.frames.right.document.write("<table border='6'><td width='150'>"+table1[i][0]+"</td><td width='150'>Offers you: £"+total_cost+"</td></tr>")
This has been bugging me for so long! I just cant seem to work out how I would be able to pull out the lowest cost and display this... Can anyone help? Sorry its not explained very well, but it might help if u see the site and the source code....
Here is the document at the moment (not finished design wise):
http://homepage.mac.com/tonyeck/new/
http://homepage.mac.com/tonyeck/new/left.html (for source of left frame if browser cant take it from the index)
Thanks guys
I have a small sorting problem within Javascript and 2D arrays. Basically i have created an array like so inside a function which calculates the final out put:
function go(){
var table1= [
["Kestrel Moon", 1165, 475, 0, 97, 0, 98],
["BB", 1231, 380, 89, 43, 180, 85],
["Whatever Your Drive", 1052, 210, 34, 23, 450, 43],
["Discount Drivers", 1667, 310, 291, 302, 189, 93],
["Indirect Curve", 804, 599, 0, 45, 0, 0],
["Swindel", 1010, 210, 187, 35, 32, 103],
["Captain", 510, 100, 0, 19, 0, 40],
["Saintly Drivers", 890, 0, 0, 0, 0, 155],
["Ipswich Union", 490, 0, 0, 0, 0, 0],
["Young Drivers", 803, 0, 203, 78, 0, 82],
["Atlee", 923, 200, 107, 81, 92, 70],
["Good Deal", 1690, 300, 434, 112, 493, 47],
["Nice Price", 775, 0, 0, 487, 0, 30],
["Insure for Less", 1435, 0, 998, 49, 0, 59],
["Instant Insure", 1223, 424, 43, 113, 68, 96]
];
The whole point of this for me is to learn javascript functions, calculations and sorting these. So the arrays basically are values for discounts from the insurance company (i.e. kestrel moon) and you are given discount say if you were above a certain age:
for (var i=0;i<table1.length;i++)
{
//______________________Defining the total cost
total_cost = table1[i][1];
//______________________If statments
//______________________If age is less than 25 - discount!
if (age > 25)
{
total_cost = total_cost - table1[i][2];
parent.frames.bottom.document.write("Over 25 years old: £"+table1[i][2]+"<BR>")
}
I have a whole bunch of If statments working out the discounts etc. Then from a form (where the user inputs data like age etc) a function is run (where all the if statements etc are kept). The results are then writen to a new frame. I want, from the output, to be able to show the user what the best price would be. The "total_cost" is worked out after all the discounts have been subtracted and was wondering how to pick out the lowest result?
This is the code which writes (and loops to write for each company) the final cost:
parent.frames.right.document.write("<table border='6'><td width='150'>"+table1[i][0]+"</td><td width='150'>Offers you: £"+total_cost+"</td></tr>")
This has been bugging me for so long! I just cant seem to work out how I would be able to pull out the lowest cost and display this... Can anyone help? Sorry its not explained very well, but it might help if u see the site and the source code....
Here is the document at the moment (not finished design wise):
http://homepage.mac.com/tonyeck/new/
http://homepage.mac.com/tonyeck/new/left.html (for source of left frame if browser cant take it from the index)
Thanks guys