Click to See Complete Forum and Search --> : Need help comparing 3 numbers...


its420here
01-26-2003, 04:05 PM
Depending on the order that I enter the 3 numbers to be compared I get either the smallest is undefined or the largest is undefined.

// compare to find the smallest of the 3 numbers
if (no1Value <= no2Value)
if (no1Value <= no3Value)
smallest = no1Value;
else
if (no2Value <= no3Value)
smallest = no2Value;
else
smallest = no3Value;

// compare to find the largest of the 3 numbers
if (no1Value >= no2Value)
if (no1Value >= no3Value)
largest = no1Value;
else
if (no2Value >= no3Value)
largest = no2Value;
else
largest = no3Value;

Any help would be appreciated.

Thank you :)

its420here
01-26-2003, 04:25 PM
Do I do the same thing with the smallest?

I am trying to find both...

its420here
01-26-2003, 04:31 PM
Oh... I didn't see the top part :D

Thanks bunches!

But could you tell me why that works and the way I had it did not???

khalidali63
01-26-2003, 04:54 PM
how about this piece..:-)


<script type="text/javascript">
var numArr = new Array(2,9,5,8,1,4);//add whatever comparison numbers you want in this array.
/****** No changes below this line please ******/
var largestNum = -999999999999,smallestNum = 999999999999;
for(n=0;n<numArr.length;n++){
if(numArr[n]<smallestNum){
smallestNum = numArr[n];
}else if(largestNum<numArr[n]){
largestNum = numArr[n];
}
}
alert("Smallest num = "+smallestNum+", and largest Num = "+largestNum);

</script>


cheers

Khalid