PDA

View Full Version : How to determine which number is highest, surprisingly hard!

questor
08-13-2004, 02:39 AM
Okay, I've got three variables: highPercent, mediumPercent, and lowPercent. The following code checks them to find the highest:

var highest = 100;
if (highPercent >= mediumPercent && highPercent >= lowPercent) { highest = highPercent; }
if (mediumPercent >= lowPercent && mediumPercent >= highPercent) { highest = mediumPercent; }
if (lowPercent >= mediumPercent && lowPercent >= highPercent) { highest = lowPercent; }

But with the following values:

High: 29
Medium: 64
Low: 7

It decides that High is the largest of the three.

Why?

jkd
08-13-2004, 02:58 AM
function max() { (arguments.slice = Array.prototype.slice)
if (arguments.length == 2)
return (arguments[0] > arguments[1]) ? arguments[0] : arguments[1];
else
return max(arguments[0], max.apply(max, arguments.slice(1)));
}

max(lowPercent, mediumPercent, highPercent)

should return the correct one.

Leave it to me to forget about the built-in Math.max() function.... Math.max(lowPercent, mediumPercent, highPercent)

jbot
08-13-2004, 09:42 AM
doesn't max only take two values tho?

Mr J
08-13-2004, 02:58 PM
I don't think so, try the following

<script>