PDA

View Full Version : Multiple arrays and comparing the FIRST and LAST btwn to get price range


BrightNail
11-21-2002, 06:56 PM
hey all,

I can do this, but I think my way would be long and ponderous.

is there a quick/efficient way to compare numerous arrays.....but only compare the 1st. and last element of each so that I get the "lowest" and "highest"...so that I can generate a range...

take my three arrays..

var price1 = new array (10.00, 11.00, 45.00, 98.00);
var price2 = new array (10.50, 11.00, 45.00,102.00);
var price3 = new array (9.00, 16.00, 47.00, 99.00);

the range would be..

9.00 - 102.00

thanks,
james

beetle
11-21-2002, 07:18 PM
You got it...var price1 = new Array (10.00, 11.00, 45.00, 98.00);
var price2 = new Array (10.50, 11.00, 45.00, 102.00);
var price3 = new Array (9.00, 16.00, 47.00, 99.00);

var smallest = getBound(price1, price2, price3, 0, 1);
var largest = getBound(price1, price2, price3, price1.length-1, 0);

function getBound(a, b, c, i, lower) {
if (lower)
return (a[i] < b[i] && a[i] < c[i]) ? a[i] : Math.min(b[i], c[i]);
else
return (a[i] > b[i] && a[i] > c[i]) ? a[i] : Math.max(b[i], c[i]);
}

BrightNail
11-23-2002, 09:30 PM
excellent..you have your sh#t together...hahahha,?:thumbsup:

great code..